plot_date problem

I found another issue with plot_date. Dont have a simple

    > example yet, and hope that this is something 'obvious' and
    > I don't have to bother.

There is clearly something wrong with the autoscale function of one of
the date tick locators. It would help to know which one. plot_date
looks at the range of your date data and tries to pick the appropriate
date tick locator based on that range (ie a YearLocator, MonthLocator,
MinuteLocator, etc). If I knew which tick locator was behaving badly,
it would help me fix the problem.

If you

  print ax.xaxis._majorLocator

after the call to plot_date, and let me know which locator it is, I
can probably figure out where the problem is. To simplify, don't
explicitly set the date xlim range when you do this.

On a side note, in your example code you call

  ax.viewLim.intervalx().set_bounds(minXValueImPlotting, maxXValueImPlotting)

I assume you did this to narrow down the possible causes of problems.
As you know, this is the call that ax.set_xlim makes under the hood.
But in general, it's safest to stick to the axes API, ie, call

  ax.set_xlim(minXValueImPlotting, maxXValueImPlotting)

since this interface is guaranteed to be stable.

JDH

Hi John:

plot_date looks at the range of your date data and tries to pick the appropriate
date tick locator based on that range (ie a YearLocator, MonthLocator,
MinuteLocator, etc).

It does?! Do you mean that this is done automatically? Can you show me an example of this using only the time module (since I use python2.2, dont have datetime)? I thought that I had to manually set things up and tell matplotlib whether to use YearLocator, MonthLocator, etc.. via calls to: axes.xaxis.set_minor_locator(), axes.xaxis.set_major_locator(), axes.xaxis.set_major_formatter().
In other words for every plot, check the time range of my data, figure out how many ticks I want, and decide whether to use months, days, hours, etc...
In fact, because I was getting some inconsistent results with using the above, I decided that for the most part (excluding a few special cases), I would print the time ticks myself 'manually'. The script below shows what I mean.

On the other note, regarding the weird scaling that I talked about (and showed pretty pics for) in my last mail, I finally put together a small script that exposes the problem. It is a bit rough because I ripped bits and pieces from here and there, but shows the issue. Use the 'wantBadPlot' and 'wantStandardDateTics' to see how things go wrong.

···

--------------------------------
#!/usr/bin/env python

import time
from matplotlib.dates import EpochConverter
from matplotlib.matlab import *
from matplotlib.ticker import FuncFormatter, NullLocator, MinuteLocator, DayLocator, HourLocator, MultipleLocator, DateFormatter

wantBadPlot=1
wantStandardDateTics=1
wantLegend=1

if wantBadPlot:
   time1=[1087192789.89]
   data1=[-65.54]
else:
   time1=[1087192289.89, 1087193789.89]
   data1=[-44.343, -65.54]
  time2=[
1087161589.89 ,
1087192289.0,
1087192389.0,
1087192489.0,
1087192589.0,
1087192689.0,
1087192789.89 ,
1087192889.0,
1087192989.0,
1087193089.0,
1087193189.0,
1087193289.0,
1087238100.0 ,
]
data2=[
-55.44
-64.54 ,
-66.54 ,
-61.54 ,
-69.54 ,
-45.66,
-55.54 ,
-77.54,
-65.54 ,
-49.54 ,
-57.54 ,
-68.54 ,
-55.54 ,
-23.44
]

ax = subplot(111)

p1Size=len(time1)
p2Size=len(time2)

p1=plot_date(time1, data1, None, '-', color='r')
p2=plot_date(time2, data2, None, '-', color='b')

if wantStandardDateTics:

   fmt=DateFormatter('%H:%M')
   hours=HourLocator(4)
     ax.xaxis.set_minor_locator(NullLocator())
   ax.xaxis.set_major_locator(hours)
   ax.xaxis.set_major_formatter(fmt)

   ax.autoscale_view()

else:
   #Manually display dates for tick-labels. Technically could use plot() and
   #get the same result.

   now=time2[-1]
   then=time2[0]
     deltaSec=now-then
   deltaTickSec=deltaSec/7.0

   tickList=[item for item in list(arange(then, now, deltaTickSec))]
     def tickString(x, pos):
       return time.strftime("%H:%M:%S", time.localtime(x))
     formatter = FuncFormatter(tickString)
     ax.set_xticks(tickList)
     ax.xaxis.set_major_formatter(formatter)
   ax.xaxis.set_minor_locator(NullLocator())
     ax.autoscale_view()

   #This will fix the problem!!
   #ax.set_xlim((then, now))

if wantLegend:
   legend((p1, p2), ('small data set (%d)' % p1Size, 'large data set (%d)' % p2Size))

xlabel('time')
grid(True)

show()
#savefig('./blah.png')
-------------------------------

Any ideas?

Finally, just want to verify (form my last email) that in axes.py:

def get_ylim(self):
     "Get the y axis range [ymin, ymax]"
     return self.viewLim.intervalx().get_bounds()

should intervax() be intervaly()??

Thanks,
--

Peter Groszkowski Gemini Observatory
Tel: +1 808 974-2509 670 N. A'ohoku Place
Fax: +1 808 935-9235 Hilo, Hawai'i 96720, USA

John Hunter wrote:

   > I found another issue with plot_date. Dont have a simple
   > example yet, and hope that this is something 'obvious' and
   > I don't have to bother.

There is clearly something wrong with the autoscale function of one of
the date tick locators. It would help to know which one. plot_date
looks at the range of your date data and tries to pick the appropriate
date tick locator based on that range (ie a YearLocator, MonthLocator,
MinuteLocator, etc). If I knew which tick locator was behaving badly,
it would help me fix the problem.

If you
print ax.xaxis._majorLocator

after the call to plot_date, and let me know which locator it is, I
can probably figure out where the problem is. To simplify, don't
explicitly set the date xlim range when you do this.

On a side note, in your example code you call

ax.viewLim.intervalx().set_bounds(minXValueImPlotting, maxXValueImPlotting)

I assume you did this to narrow down the possible causes of problems.
As you know, this is the call that ax.set_xlim makes under the hood.
But in general, it's safest to stick to the axes API, ie, call

ax.set_xlim(minXValueImPlotting, maxXValueImPlotting)

since this interface is guaranteed to be stable.

JDH

-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the
one installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users