I've got an MPL widget embedded in a qt application.  The user interacts w/
the GUI and causes data to be shown or hidden in the plot.  Since the data
can be very large (5-10 lines w/ 40,000 points in each), I've written code
that reads the data and builds Line2D objects.  Then I call axes.add_line()
and line.remove() to add and remove them from my plot.

This works fine when the x axis is a floating point axis.  If I switch the x
axis to dates, then when I try to re-add some removed lines, I get the error
at the bottom of the email.  A script that duplicates this is attached
(though I think you need to have an interactive mode session running to
really see what happens.  

As an example:

1) read data and build 4 Line2D objects
2) user shows lines 1 and 2 
3) user hides lines 1 and 2, shows lines 3 and 4
4) user hides lines 3 and 4, shows lines 1 and 2

These steps work fine w/ floats.  But if I tell the xaxis to be date
(axis.xaxis_date()), then step 4 above will give the error:

  File "./error.py", line 62, in <module>
    run( True )
  File "./error.py", line 53, in run
    ax.autoscale_view()
  File
"/group/monte/development/rh8/tools.2007_03_13/lib/python2.5/site-packages/m
atplotlib/axes.py", line 1268, in autoscale_view
    XL = self.xaxis.get_major_locator().autoscale()
  File
"/group/monte/development/rh8/tools.2007_03_13/lib/python2.5/site-packages/m
atplotlib/dates.py", line 568, in autoscale
    dmin, dmax = self.datalim_to_dt()
  File
"/group/monte/development/rh8/tools.2007_03_13/lib/python2.5/site-packages/m
atplotlib/dates.py", line 434, in datalim_to_dt
    return num2date(dmin, self.tz), num2date(dmax, self.tz)
  File
"/group/monte/development/rh8/tools.2007_03_13/lib/python2.5/site-packages/m
atplotlib/dates.py", line 234, in num2date
    if not cbook.iterable(x): return _from_ordinalf(x, tz)
  File
"/group/monte/development/rh8/tools.2007_03_13/lib/python2.5/site-packages/m
atplotlib/dates.py", line 157, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1

Any help would be greatly appreciated.  It almost seems like using date mode
is somehow changing the Line2D object.  I tried not clearing the figure and
just removing the lines, but that causes other problems (the plot won't
autoscale in step 4 and I also need to toggle other items on and off -
legends and things).

Thanks,
Ted
#!/bin/env python

import pylab as p
from matplotlib.lines import Line2D

def run( usedate ):
   t0 = 730485.0
   lines1 = [
      Line2D( [t0,t0+1], [2,3], linestyle = "", marker = 'x' ),
      Line2D( [t0+2,t0+3], [-2,-3], linestyle = "", marker = 'x' ),
      ]

   lines2 = [
      Line2D( [t0+4,t0+5], [4,5], linestyle = "", marker = 'x' ),
      Line2D( [t0+6,t0+7], [-4,-5], linestyle = "", marker = 'x' ),
      ]


   fig = p.figure()
   ax = p.gca()
   if usedate:
      ax.xaxis_date()

   for l in lines1:
      ax.add_line( l )

   ax.autoscale_view()
   fig.show()

   raw_input("press return...")

   fig.clear()
   ax = fig.gca()
   if usedate:
      ax.xaxis_date()

   for l in lines2:
      ax.add_line( l )

   ax.autoscale_view()
   ax.figure.canvas.draw()

   raw_input("press return...")

   fig.clear()
   ax = fig.gca()
   if usedate:
      ax.xaxis_date()

   for l in lines1:
      ax.add_line( l )

   ax.autoscale_view()
   ax.figure.canvas.draw()

   raw_input("press return...")
   
# Run w/o dates - works fine
run( False )

# run w/ dates - errors
run( True )
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to