4

I am trying to plot time-series data in Python using ggplot and am unable to fix the scale.

Here's my most recent effort--first I set the desired max and min values of the x axis as xmin and xmax:

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

Then, from dataframe fishdf I try to plot my time variable ('tottime'--the x-axis) against a numeric variable ('rx', the y-axis):

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)   

This works fine when I don't include the limits= command, but with the limits I get an error

TypeError: a float is required

I have tried various ways of setting/formatting the xmin and xmax, but can't seem to get this to work. Is there a simple solution? I have seen related questions elsewhere but the answers don't seem to work for me (or there are no answers?)

TCS
  • 451
  • 6
  • 18

1 Answers1

1

As far as I have been able to determine, this is a bug in Python's ggplot. I was able to port my data over to R and run very similar code which worked. IT's more complicated than the following (multiple else's), but in essence this code worked and allowed me to generate 500 plots from 4 data types, all with common axes. So to be clear, this is R code, not 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.
TCS
  • 451
  • 6
  • 18