218 lines (174 with data), 18.3 kB
@header@
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="matplotlib.html"><font color="#ffffff">matplotlib</font></a>.units</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/jdhunter/dev/lib64/python2.5/site-packages/matplotlib/units.py">/home/jdhunter/dev/lib64/python2.5/site-packages/matplotlib/units.py</a></font></td></tr></table>
<p><tt>The classes here provide support for using custom classes with<br>
matplotlib, eg those that do not expose the array interface but know<br>
how to converter themselves to arrays. It also supoprts classes with<br>
units and units conversion. Use cases include converters for custom<br>
objects, eg a list of datetime objects, as well as for objects that<br>
are unit aware. We don't assume any particular units implementation,<br>
rather a units implementation must provide a <a href="#ConversionInterface">ConversionInterface</a>, and<br>
the register with the <a href="#Registry">Registry</a> converter dictionary. For example,<br>
here is a complete implementation which support plotting with native<br>
datetime objects<br>
<br>
<br>
import matplotlib.units as units<br>
import matplotlib.dates as dates<br>
import matplotlib.ticker as ticker<br>
import datetime<br>
<br>
class DateConverter(units.<a href="#ConversionInterface">ConversionInterface</a>):<br>
<br>
def convert(value, unit):<br>
'convert value to a scalar or array'<br>
return dates.date2num(value)<br>
convert = staticmethod(convert)<br>
<br>
def axisinfo(unit):<br>
'return major and minor tick locators and formatters'<br>
if unit!='date': return None<br>
majloc = dates.AutoDateLocator()<br>
majfmt = dates.AutoDateFormatter(majloc)<br>
return <a href="#AxisInfo">AxisInfo</a>(majloc=majloc,<br>
majfmt=majfmt,<br>
label='date')<br>
axisinfo = staticmethod(axisinfo)<br>
<br>
<br>
def default_units(x):<br>
'return the default unit for x or None'<br>
return 'date'<br>
default_units = staticmethod(default_units)<br>
<br>
# finally we register our object type with a converter<br>
units.registry[datetime.date] = DateConverter()</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="__builtin__.html#dict">__builtin__.dict</a>(<a href="__builtin__.html#object">__builtin__.object</a>)
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="matplotlib.units.html#Registry">Registry</a>
</font></dt></dl>
</dd>
<dt><font face="helvetica, arial"><a href="matplotlib.units.html#AxisInfo">AxisInfo</a>
</font></dt><dt><font face="helvetica, arial"><a href="matplotlib.units.html#ConversionInterface">ConversionInterface</a>
</font></dt></dl>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="AxisInfo">class <strong>AxisInfo</strong></a></font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt>information to support default axis labeling and tick labeling<br> </tt></td></tr>
<tr><td> </td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="AxisInfo-__init__"><strong>__init__</strong></a>(self, majloc<font color="#909090">=None</font>, minloc<font color="#909090">=None</font>, majfmt<font color="#909090">=None</font>, minfmt<font color="#909090">=None</font>, label<font color="#909090">=None</font>)</dt><dd><tt>majloc and minloc: TickLocators for the major and minor ticks<br>
majfmt and minfmt: TickFormatters for the major and minor ticks<br>
label: the default axis label<br>
<br>
If any of the above are None, the axis will simply use the default</tt></dd></dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="ConversionInterface">class <strong>ConversionInterface</strong></a></font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt>The minimal interface for a converter to take custom instances (or<br>
sequences) and convert them to values mpl can use<br> </tt></td></tr>
<tr><td> </td>
<td width="100%">Static methods defined here:<br>
<dl><dt><a name="ConversionInterface-axisinfo"><strong>axisinfo</strong></a>(unit)</dt><dd><tt>return an units.<a href="#AxisInfo">AxisInfo</a> instance for unit</tt></dd></dl>
<dl><dt><a name="ConversionInterface-convert"><strong>convert</strong></a>(obj, unit)</dt><dd><tt>convert obj using unit. If obj is a sequence, return the<br>
converted sequence. The ouput must be a sequence of scalars<br>
that can be used by the numpy array layer</tt></dd></dl>
<dl><dt><a name="ConversionInterface-default_units"><strong>default_units</strong></a>(x)</dt><dd><tt>return the default unit for x or None</tt></dd></dl>
<dl><dt><a name="ConversionInterface-is_numlike"><strong>is_numlike</strong></a>(x)</dt><dd><tt>The matplotlib datalim, autoscaling, locators etc work with<br>
scalars which are the units converted to floats given the<br>
current unit. The converter may be passed these floats, or<br>
arrays of them, even when units are set. Derived conversion<br>
interfaces may opt to pass plain-ol unitless numbers through<br>
the conversion interface and this is a helper function for<br>
them.</tt></dd></dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="Registry">class <strong>Registry</strong></a>(<a href="__builtin__.html#dict">__builtin__.dict</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt>register types with conversion interface<br> </tt></td></tr>
<tr><td> </td>
<td width="100%"><dl><dt>Method resolution order:</dt>
<dd><a href="matplotlib.units.html#Registry">Registry</a></dd>
<dd><a href="__builtin__.html#dict">__builtin__.dict</a></dd>
<dd><a href="__builtin__.html#object">__builtin__.object</a></dd>
</dl>
<hr>
Methods defined here:<br>
<dl><dt><a name="Registry-__init__"><strong>__init__</strong></a>(self)</dt></dl>
<dl><dt><a name="Registry-get_converter"><strong>get_converter</strong></a>(self, x)</dt><dd><tt>get the converter interface instance for x, or None</tt></dd></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary for instance variables (if defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list of weak references to the object (if defined)</tt></dd>
</dl>
<hr>
Methods inherited from <a href="__builtin__.html#dict">__builtin__.dict</a>:<br>
<dl><dt><a name="Registry-__cmp__"><strong>__cmp__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__cmp__">__cmp__</a>(y) <==> cmp(x,y)</tt></dd></dl>
<dl><dt><a name="Registry-__contains__"><strong>__contains__</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-__contains__">__contains__</a>(k) -> True if D has a key k, else False</tt></dd></dl>
<dl><dt><a name="Registry-__delitem__"><strong>__delitem__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__delitem__">__delitem__</a>(y) <==> del x[y]</tt></dd></dl>
<dl><dt><a name="Registry-__eq__"><strong>__eq__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__eq__">__eq__</a>(y) <==> x==y</tt></dd></dl>
<dl><dt><a name="Registry-__ge__"><strong>__ge__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__ge__">__ge__</a>(y) <==> x>=y</tt></dd></dl>
<dl><dt><a name="Registry-__getattribute__"><strong>__getattribute__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__getattribute__">__getattribute__</a>('name') <==> x.name</tt></dd></dl>
<dl><dt><a name="Registry-__getitem__"><strong>__getitem__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__getitem__">__getitem__</a>(y) <==> x[y]</tt></dd></dl>
<dl><dt><a name="Registry-__gt__"><strong>__gt__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__gt__">__gt__</a>(y) <==> x>y</tt></dd></dl>
<dl><dt><a name="Registry-__hash__"><strong>__hash__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__hash__">__hash__</a>() <==> hash(x)</tt></dd></dl>
<dl><dt><a name="Registry-__iter__"><strong>__iter__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__iter__">__iter__</a>() <==> iter(x)</tt></dd></dl>
<dl><dt><a name="Registry-__le__"><strong>__le__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__le__">__le__</a>(y) <==> x<=y</tt></dd></dl>
<dl><dt><a name="Registry-__len__"><strong>__len__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__len__">__len__</a>() <==> len(x)</tt></dd></dl>
<dl><dt><a name="Registry-__lt__"><strong>__lt__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__lt__">__lt__</a>(y) <==> x<y</tt></dd></dl>
<dl><dt><a name="Registry-__ne__"><strong>__ne__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__ne__">__ne__</a>(y) <==> x!=y</tt></dd></dl>
<dl><dt><a name="Registry-__repr__"><strong>__repr__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__repr__">__repr__</a>() <==> repr(x)</tt></dd></dl>
<dl><dt><a name="Registry-__setitem__"><strong>__setitem__</strong></a>(...)</dt><dd><tt>x.<a href="#Registry-__setitem__">__setitem__</a>(i, y) <==> x[i]=y</tt></dd></dl>
<dl><dt><a name="Registry-clear"><strong>clear</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-clear">clear</a>() -> None. Remove all items from D.</tt></dd></dl>
<dl><dt><a name="Registry-copy"><strong>copy</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-copy">copy</a>() -> a shallow copy of D</tt></dd></dl>
<dl><dt><a name="Registry-get"><strong>get</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-get">get</a>(k[,d]) -> D[k] if k in D, else d. d defaults to None.</tt></dd></dl>
<dl><dt><a name="Registry-has_key"><strong>has_key</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-has_key">has_key</a>(k) -> True if D has a key k, else False</tt></dd></dl>
<dl><dt><a name="Registry-items"><strong>items</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-items">items</a>() -> list of D's (key, value) pairs, as 2-tuples</tt></dd></dl>
<dl><dt><a name="Registry-iteritems"><strong>iteritems</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-iteritems">iteritems</a>() -> an iterator over the (key, value) items of D</tt></dd></dl>
<dl><dt><a name="Registry-iterkeys"><strong>iterkeys</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-iterkeys">iterkeys</a>() -> an iterator over the keys of D</tt></dd></dl>
<dl><dt><a name="Registry-itervalues"><strong>itervalues</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-itervalues">itervalues</a>() -> an iterator over the values of D</tt></dd></dl>
<dl><dt><a name="Registry-keys"><strong>keys</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-keys">keys</a>() -> list of D's keys</tt></dd></dl>
<dl><dt><a name="Registry-pop"><strong>pop</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-pop">pop</a>(k[,d]) -> v, remove specified key and return the corresponding value<br>
If key is not found, d is returned if given, otherwise KeyError is raised</tt></dd></dl>
<dl><dt><a name="Registry-popitem"><strong>popitem</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-popitem">popitem</a>() -> (k, v), remove and return some (key, value) pair as a<br>
2-tuple; but raise KeyError if D is empty</tt></dd></dl>
<dl><dt><a name="Registry-setdefault"><strong>setdefault</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-setdefault">setdefault</a>(k[,d]) -> D.<a href="#Registry-get">get</a>(k,d), also set D[k]=d if k not in D</tt></dd></dl>
<dl><dt><a name="Registry-update"><strong>update</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-update">update</a>(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]<br>
(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]</tt></dd></dl>
<dl><dt><a name="Registry-values"><strong>values</strong></a>(...)</dt><dd><tt>D.<a href="#Registry-values">values</a>() -> list of D's values</tt></dd></dl>
<hr>
Data and other attributes inherited from <a href="__builtin__.html#dict">__builtin__.dict</a>:<br>
<dl><dt><strong>__new__</strong> = <built-in method __new__ of type object at 0x3f8ab3c120><dd><tt>T.<a href="#Registry-__new__">__new__</a>(S, ...) -> a new object with type S, a subtype of T</tt></dl>
<dl><dt><strong>fromkeys</strong> = <built-in method fromkeys of type object at 0x21a4480><dd><tt><a href="__builtin__.html#dict">dict</a>.<a href="#Registry-fromkeys">fromkeys</a>(S[,v]) -> New <a href="__builtin__.html#dict">dict</a> with keys from S and values equal to v.<br>
v defaults to None.</tt></dl>
</td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#55aa55">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
<tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td>
<td width="100%"><strong>registry</strong> = {}</td></tr></table>
@footer@