我正在尝试使用 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= 命令时,这工作正常,但有限制时我得到一个错误
类型错误:需要一个 float
我已经尝试了各种设置/格式化 xmin 和 xmax 的方法,但似乎无法使它起作用。有一个简单的解决方案吗?我在其他地方看到过相关问题,但答案似乎对我不起作用(或者没有答案?)
最佳答案
据我所知,这是 Python 的 ggplot 中的错误。我能够将我的数据移植到 R 并运行非常相似的有效代码。它比下面的(其他多个)更复杂,但本质上这段代码有效并允许我从 4 种数据类型生成 500 个图,所有图都具有公共(public)轴。明确一点,这是 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.
关于python ggplot为日期时间设置轴范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680066/