2014-10-31 123 views
2

我想绘制使用ggplot在Python中的时间序列数据,我无法修复的规模。蟒蛇ggplot设置日期时间轴的范围

这是我最近的努力 - 首先我设置所需max和x轴的XMIN的最小值和XMAX:

xmin=pd.to_datetime('2011-04-01')
xmax=pd.to_datetime('2011-08-01')

然后,从数据帧fishdf我尝试绘图我的时间变量(” tottime“ - x轴)相对于一个数字变量(” RX”,y轴):

fig=(ggplot(fishdf,aes('tottime','rx')) + \ 
    geom_line() + \ 
    geom_point() + \ 
    ggtitle(PIT) + \ 
    scale_x_date(breaks='7 days', 
     labels=date_format('%m -%d'), 
     limits=(xmin,xmax))) + \ 
    scale_y_continuous(limits=(0,235)) 
outfig= r"C:\a\Projects\Shad Telemetry\Connecticut River\CumDat\Python\figs\%s.png"%(PIT) 
ggsave(fig,outfig) 

能正常工作的时候,不包括的限值=命令,但与限制我出现错误

TypeError:需要一个浮点数

我已经尝试过设置/格式化xmin和xmax的各种方式,但似乎无法得到此工作。有一个简单的解决方案吗?我在其他地方看过相关的问题,但答案似乎不适用于我(或者没有答案?)

回答

0

就我所能确定的,这是Python的ggplot中的一个错误。我能够将数据移植到R并运行非常类似的代码。 IT比以下更复杂(其他的),但实际上这个代码工作并允许我从4种数据类型生成500个图表,所有这些图表都具有公共坐标轴。所以要清楚,这是R代码,而不是Python:

xlimits<-as.POSIXct(c('2011-04-15','2011-08-01'),tz='GMT') 
for(i in as.vector(PITs2011$PIT)) 
{ 
    plot<-paste("C:\\etc\\",i,".png", sep="") 
    png(plot,width=7.5, height=5, units="in",res=300) 
    title=paste(i,'Release Location=',Tags$ReleaseLocation[Tags$PIT==i]) 
    Tagi=Tags[Tags$PIT==i,] 
    PITi=Clean2011[Clean2011$PIT==i,] #THIS SHOULD REALLY BE Radioi 
    nr<-nrow(PITi) 
    TIRISi=FWRES[FWRES$PIT==i,] 
    nt<-nrow(TIRISi) 
    Mobilei=Mobile[Mobile$PIT==i,] 
    nm<-nrow(Mobilei) 
    p<-'' 
    if((nt==0) & (nm==0) & (nr==0)) { #First group has only radio data and also Tags data (true for all) 
    p<-ggplot(data=Tagi,aes(x=time, y=rx)) +#Need to modify this for fish with only ReleaseTime (no radio) 
    geom_point(data=Tagi,aes(color='PIT'), shape=21, size=4) + 
    scale_colour_manual(name="", 
     values = c("Radio"="blue", "PIT"="red"))+ 
    scale_x_datetime(limits=xlimits)+ 
    scale_y_continuous(limits=c(0,235))+ 
    ggtitle(title) 
    }else if # etc...I then subsetted for the various data types that were available for each plot.