python ggplot切断微秒

时间:2016-10-28 11:43:23

标签: python pandas python-ggplot

我正在尝试将以下r代码移植到python。

require(ggplot2)
sdf = read.csv('sdf.csv', as.is = T, header = T)
sdf$TIMESTAMP = as.POSIXct(strptime(sdf$TIMESTAMP, "%Y-%m-%d %H:%M:%OS"))

lower = as.POSIXct(strftime(min(sdf$TIMESTAMP),"%Y-%m-%d"))
limits = c(lower + 9*3600+15*60, lower + 15*3600+30*60)
ggplot(sdf, aes(x = TIMESTAMP, y = Value, col = optype)) + 
  geom_point() + labs(x = "Time", y = 'Last Traded Price') + 
  ggtitle(paste('LTP for strike of', sdf$strike[1], 'on', 
                strftime(sdf$TIMESTAMP[1], format = '%Y-%m-%d') )) +
  facet_wrap(~optype, scales = "free_y") + theme_bw() + scale_colour_discrete(guide = FALSE) + 
  scale_x_datetime(breaks = date_breaks("30 min"), 
                   labels = date_format("%H:%M"), limits = limits) + 
  theme(strip.text.x = element_text(size = 16, colour = "white"), 
        strip.background = element_rect(fill = "black"),
        axis.text.x = element_text(angle = 90, hjust = 1, size=14, color="black"),
        axis.title = element_text(size = rel(1.6), face="bold"), 
        plot.title = element_text(size = rel(1.8)) )

python代码片段如下所示:

import pandas as pd
from ggplot import *

sdf = pd.read_csv('sdf.csv') # read csv
sdf['TIMESTAMP'] = pd.to_datetime(sdf['TIMESTAMP'], errors='coerce')

ggplot(sdf, aes(x = 'TIMESTAMP', y = 'Value', color = 'optype')) + \
  geom_point() + labs(x = "Time", y = 'Last Traded Price') + \
  ggtitle('LTP for strike of 8600.0') + \
  theme_bw() + \
  scale_x_date(labels = date_format("%H:%M"))

数据sdf如下:

"strike","TIMESTAMP","optype","Value","Item"
8600,2016-10-26 13:40:40,"call",54.3,"LTP"
8600,2016-10-26 13:22:51,"call",57.6,"LTP"
8600,2016-10-26 13:31:19,"call",53.2,"LTP"
8600,2016-10-26 14:45:13,"call",39.45,"LTP"
8600,2016-10-26 14:41:40,"call",39,"LTP"
8600,2016-10-26 14:34:11,"call",40.8,"LTP"
8600,2016-10-26 12:48:33,"call",55.2,"LTP"
8600,2016-10-26 13:01:00,"call",48.6,"LTP"
8600,2016-10-26 14:22:34,"call",47.35,"LTP"
8600,2016-10-26 13:58:43,"call",53.55,"LTP"
8600,2016-10-26 12:40:20,"call",53.8,"LTP"
8600,2016-10-26 14:20:50,"call",51.05,"LTP"
8600,2016-10-26 14:25:49,"call",43.7,"LTP"
8600,2016-10-26 14:37:46,"call",42.55,"LTP"
8600,2016-10-26 15:06:52,"call",37.25,"LTP"
8600,2016-10-26 12:57:58,"call",46.95,"LTP"
8600,2016-10-26 13:36:42,"call",54.95,"LTP"
8600,2016-10-26 15:13:22,"call",37.65,"LTP"
8600,2016-10-26 14:44:24,"call",38.7,"LTP"
8600,2016-10-26 13:22:38,"call",57.6,"LTP"
8600,2016-10-26 13:40:40,"put",18.15,"LTP"
8600,2016-10-26 13:22:51,"put",18.35,"LTP"
8600,2016-10-26 13:31:19,"put",18.4,"LTP"
8600,2016-10-26 14:45:13,"put",23.5,"LTP"
8600,2016-10-26 14:41:40,"put",24.2,"LTP"
8600,2016-10-26 14:34:11,"put",23.7,"LTP"
8600,2016-10-26 12:48:33,"put",19.1,"LTP"
8600,2016-10-26 13:01:00,"put",21.75,"LTP"
8600,2016-10-26 14:22:34,"put",19.6,"LTP"
8600,2016-10-26 13:58:43,"put",18,"LTP"
8600,2016-10-26 12:40:20,"put",19.95,"LTP"
8600,2016-10-26 14:20:50,"put",18.6,"LTP"
8600,2016-10-26 14:25:49,"put",21.65,"LTP"
8600,2016-10-26 14:37:46,"put",21.7,"LTP"
8600,2016-10-26 15:06:52,"put",22.2,"LTP"
8600,2016-10-26 12:57:58,"put",22.7,"LTP"
8600,2016-10-26 13:36:42,"put",17.55,"LTP"
8600,2016-10-26 15:13:22,"put",20.45,"LTP"
8600,2016-10-26 14:44:24,"put",24.95,"LTP"
8600,2016-10-26 13:22:38,"put",18.35,"LTP"

r片段工作正常。但是python代码会删除所有与时间相关的信息,因此时间戳变为00:00:00,因此所有数据点都以垂直线出现。如何防止这种情况?

0 个答案:

没有答案