python ggplot设置datetime的轴范围

时间:2014-10-31 17:13:33

标签: python datetime limits python-ggplot

我正在尝试使用ggplot在Python中绘制时间序列数据,但无法修复比例。

这是我最近的努力 - 首先我将x轴的所需最大值和最小值设置为xmin和xmax:

  

XMIN = pd.to_datetime( '2011-04-01')
  XMAX = pd.to_datetime( '2011-08-01')

然后,从dataframe 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)   

当我不包含limits =命令时,这可以正常工作,但是我有一个错误

TypeError:需要浮点数

我尝试了各种设置/格式化xmin和xmax的方法,但似乎无法使其工作。有简单的解决方案吗?我在其他地方看过相关的问题,但答案对我来说似乎没有用(或者没有答案?)

1 个答案:

答案 0 :(得分:1)

据我所知,这是Python的ggplot中的一个错误。我能够将我的数据移植到R并运行非常相似的代码。它比以下(其他多个)更复杂,但实质上这个代码工作,并允许我从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.