[Matplotlib-devel] Units discussion...

Jody Klymak jklymak at uvic.ca
Fri Feb 9 12:22:54 EST 2018



> On 9 Feb 2018, at 03:55, David Stansby <dstansby at gmail.com> wrote:
> 
> After having thought about this a bit more, apologies for jumping the gun.
> 
> I'm trying to learn more about how the units system works at the moment and why it works that way. I'll probably try and improve the docs a bit as I go.
> 
> One question I have at the moment is why do some plotting methods pass units through them quite a long way, instead of just doing the conversion right at the beginning of the method? It seems like the obvious thing to do to me is do the conversion immediately, but I'm sure there must be a good reason to pass units through in some places.

So that something like 

```python
ax.plot(a, b, units=‘inches’)
ax.plot(a, c, units=‘centimeters’)
```

will convert to centimeters at draw time.  This currently works with `plot` because `Line2D` objects carry the units through until the path gets cached.  It doesn’t work for `scatter`, though it seems possible that it could, but maybe only with violence to how `scatter` is architectured.

I was thinking of building a units tutorial on this, with what works and what doesn’t work documented.  But if you are eager to do it, then that’d be great.  

I found this to be a reasonable set of examples:

```python
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.testing.jpl_units as units
units.register()

xdata = [x*units.sec for x in range(10)]
xdata = np.arange(10) * units.sec
ydata1 = (1.5*np.arange(10) - 0.5) * units.km
ydata2 = (1.75*np.arange(10) - 1.0) * units.km

fig, ax = plt.subplots()
ax.plot(xdata, ydata1, color='blue', xunits="sec")
ax.set_xlabel("x-label 001")

fig, ax = plt.subplots()
ax.plot(xdata, ydata1, color='blue', xunits="hour")
ax.set_xlabel("hours")

fig, ax = plt.subplots()
ax.plot(xdata, ydata1, color='blue', xunits="sec", yunits='m')
ax.plot(xdata, ydata2, color='green', xunits="hour")
ax.set_xlabel("hours")
plt.show()

```


> 
> David
> 
> On 9 February 2018 at 00:33, Jody Klymak <jklymak at uvic.ca <mailto:jklymak at uvic.ca>> wrote:
> 
> 
>> On 8 Feb 2018, at 15:18, Drain, Theodore R (392P) <theodore.r.drain at jpl.nasa.gov <mailto:theodore.r.drain at jpl.nasa.gov>> wrote:
>> 
>> On 8 February 2018 at 21:47, Drain, Theodore R (392P) <theodore.r.drain at jpl.nasa.gov <mailto:theodore.r.drain at jpl.nasa.gov><mailto:theodore.r.drain at jpl.nasa.gov <mailto:theodore.r.drain at jpl.nasa.gov>>> wrote:
>> FYI for anyone interested - we already submitted (around the time of the first unit submit code in 2009) a mock of up our unit and time classes, with converters and tickers which is located in matplotlib/testing/jpl_units/.  It doesn't appear to be used in any tests anymore but it's there if anyone wants to look at it and was used in the original unit API testing.
> 
> 
> Aha! `jpl_units` *is* used for a few tests in `test_axes.py` and `test_dates.py`, and `test_patches.py`
> 
> And those tests behave in what I’d say is a reasonable manner.  So maybe step 1 of coming up with a decent toy units system is accomplished.  
> 
> Steps 2 and 3 might be to add some more tests and some documentation/tutorial using the toy unit system.  I’m happy to give that a shot in the next few weeks.  Then we can move on to fixing the methods that don’t play well w/ units.  
> 
> Cheers,   Jody
> 
> 
> 
> 
> 
> 
> 

--
Jody Klymak    
http://web.uvic.ca/~jklymak/





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20180209/697fec22/attachment.html>


More information about the Matplotlib-devel mailing list