On Sun, Sep 20, 2009 at 6:50 PM, Drain, Theodore R (343P) <theodore.r.dr...@jpl.nasa.gov> wrote:
> I've run into this problem quite a few times and I'd love to figure out some > way to fix it. As an example, here's the kind of scenario this occurs in: > > I embed MPL in a few different GUI's that plot data either in real-time or > via the user selecting things. There is a saved state which contains > preferences like auto-scaling, legend on/off, axis formatting, etc. When the > app starts up, I need to create a plot to put on the screen and configure it. > What I'd like to do is this: > > - create widget > - apply format (date formatter, etc) > - apply settings (autoscale, etc) > - wait for data (either via real time feed or user clicking on things) > > But this is impossible because of this kind of bug. Instead, I have to > create a plot with a fake date range and test every operation to see if it's > actually setting data before applying the settings like autoscale. In > addition, if the user removes data from the plot (via menu or selectable > lists), I have to either start over or "unset" the settings back to something > safe so this error won't occur. It really makes coding something like this a > royal pain. > > I don't have a suggestion as of yet... Perhaps it could just return "N/A" or > something like that. > > I think part of the problem might be the default ranges used by the > autoscaling algorithm when there is no data are invalid for certain > formatters and locators. That suggests that possible solutions might be one > of: > > 1) require autoscaling or scaling algorithms to return ranges that will be OK > for known scalers/formatters. Perhaps some system that allows different > autoscaling algorithms to be set which can configure the default? > 2) require scalers/formatters to be robust for any range or engineer the > system to allow them to report "errors" in a way that allows the plot do > something reasonable and not trigger an exception (perhaps some changeable > behavior w/ the default as an exception?). > > I'll think about this a little this week and see if any other ideas come to > mind. I think we have this problem mostly licked. The problem I was writing about in my email is a 2nd tier problem. For example, in svn HEAD, you can specify an "empty" date plot as long as you inform mpl of you intentions.. From the test_date_empty unit test:: fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.xaxis_date() fig.savefig('date_empty') Here we are fine, because we call ax.axaxis_date, which informs mpl that you intend to pass in datetime instances. The key piece which makes this possible, which you allude to in your post, is the default xlimits, which is new in svn HEAD. In particular, the default converter provides an AxisInfo which now supports an optional attribute default_limits: the default min, max of the axis if no data is present which is overridden in the DateConverter: def axisinfo(unit, axis): 'return the unit AxisInfo' # make sure that the axis does not start at 0 majloc = AutoDateLocator(tz=unit) majfmt = AutoDateFormatter(majloc, tz=unit) datemin = datetime.date(2000, 1, 1) datemax = datetime.date(2010, 1, 1) return units.AxisInfo( majloc=majloc, majfmt=majfmt, label='', default_limits=(datemin, datemax)) while the min/max are arbitrary, the important thing is that custom types can now handle the default min/max limits, so when you present a new type to mpl, the type can request a certain default view/data lim if no data are presented. Additionally, because of the "ignore"setting on the limts argument, we can detect whether the limits we are applying are defaults or actively set by the user. The complication that motivated the sf bug http://sourceforge.net/tracker/?func=detail&aid=2861426&group_id=80706&atid=560720 is a bit more subtle. Here no data type is presented to mpl -- either through "plot" or "fill" or "set_xlim" or whatever. If the user had passed any data in, or manually expressed their intent through "ax.xaxis_date" we would be fine. The difficulty is that they passed no data in but declared their intention to use a "YearFormatter". My original inclination, and the one that failed the unit tests, was to trigger a call to Axis.axis_date (a new method) on a call to ax.xaxis.set_major_formatter (or locator) where the argument was a DateLocator or DateFormatter. This seemed to be an imminently reasonable and helpful thing to do -- if they want a date locator or formatter presumably they will be passing in dates -- but the unit tests told me this was wrong. The locators and formatters work on *converted* units. The EpochConverter and DateConverter both convert their native types to floating point days since 0000-00-00. So here are two custom converter interfaces which both end up with the same floating point representation. The conclusion is: mpl cannot use the locator/formatter type to infer what the basic type that users will be passing in. Just because two classes end up with the same floating point representation does not indicate that they want the same conversion pipeline from type -> float. Nonetheless, we can, and already do in svn HEAD, handle the cases that I think you are worried about in this email. As long as you know what type you will be passing into mpl (regardless of whether you have any data available right now) you can inform the units interface with ax.xaxis.update_units(someval) where someval is an instance of the type you plan to pass in. Doing so will not affect your current data or view limits, but will trigger the conversion interface and importantly will trigger the units.AxisInfo.default_limits scaling which was recently added to avoid the kinds of problems we have been seeing with date conversion when no data is passed in. So despite this long winded email, the current infrastructure should support * create axes, etc * set your current formatter/locator * ax.xaxis.update_units(myInstance) where myInstance is an object of the type you expect to pass in. As long as you have registered a converter from type(myInstance) -> ConversionInterface, you can now specify the default limits through the ConversionInterface.default_limits method:: @staticmethod def default_units(x, axis): 'return the default unit for x or None for the given axis' return None As an example in matplotlib.dates, we choose an arbitrary interval, which while arbitrary avoids the 0..1 problem we have been having:: class DateConverter(units.ConversionInterface): """The units are equivalent to the timezone.""" @staticmethod def axisinfo(unit, axis): 'return the unit AxisInfo' # make sure that the axis does not start at 0 majloc = AutoDateLocator(tz=unit) majfmt = AutoDateFormatter(majloc, tz=unit) datemin = datetime.date(2000, 1, 1) datemax = datetime.date(2010, 1, 1) return units.AxisInfo( majloc=majloc, majfmt=majfmt, label='', default_limits=(datemin, datemax)) JDH ------------------------------------------------------------------------------ Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel