【问题标题】:python ggplot set axis range for datetimepython ggplot设置日期时间的轴范围
【发布时间】:2014-10-31 17:13:32
【问题描述】:

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

这是我最近的努力——首先我将 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)   

当我不包含 limits= 命令时,这可以正常工作,但使用限制我会收到错误

TypeError:需要浮点数

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

【问题讨论】:

    标签: python datetime limits python-ggplot


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多