python ggplot设置datetim的轴范围

2024-07-07 00:01:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我最近的努力——首先,我将x轴的期望最大值和最小值设置为xmin和xmax:

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

然后,在dataframe fishdf中,我尝试根据数值变量(“rx”,y轴)绘制时间变量(“tottime”--x轴):

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=命令时,这很好,但是使用limits时,我得到了一个错误

类型错误:需要浮点

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


Tags: todatetime时间fig绘制rxpdxmin
1条回答
网友
1楼 · 发布于 2024-07-07 00:01:09

据我所知,这是Python ggplot中的一个bug。我可以将我的数据移植到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.

相关问题 更多 >