2014-10-31 124 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.