The plot and scatter methods now deal nicely with Numpy array sub-classes, such as Astropy quantities (which have attached units):
In [1]: fromastropyimportunitsasuIn [2]: importmatplotlib.pyplotaspltIn [3]: fig=plt.figure()
In [4]: ax=fig.add_subplot(1,1,1)
In [5]: x= [1,2] *u.mIn [6]: y= [3,4] *u.mIn [7]: ax.plot(x,y)
Out[7]: [<matplotlib.lines.Line2Dat0x107f00048>]
In [8]: ax.scatter(x, y)
Out[8]: <matplotlib.collections.PathCollectionat0x107f00ba8>
However, other methods such as axhline and axvline do not:
In [9]: z=4*u.mIn [10]: ax.axhline(z)
---------------------------------------------------------------------------UnitsErrorTraceback (mostrecentcalllast)
<ipython-input-10-39d10b51e434>in<module>()
---->1ax.axhline(z)
/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/matplotlib/axes/_axes.pyinaxhline(self, y, xmin, xmax, **kwargs)
744self._process_unit_info(ydata=y, kwargs=kwargs)
745yy=self.convert_yunits(y)
-->746scaley= (yy<ymin) or (yy>ymax)
747748trans=self.get_yaxis_transform(which='grid')
/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/astropy/units/quantity.pyin__array_prepare__(self, obj, context)
344"argument is not a quantity (unless the "345"latter is all zero/infinity/nan)"-->346 .format(function.__name__))
347exceptTypeError:
348# _can_have_arbitrary_unit failed: arg could not be comparedUnitsError: Canonlyapply'less'functiontodimensionlessquantitieswhenotherargumentisnotaquantity (unlessthelatterisallzero/infinity/nan)
It would be nice to try and make these methods also support Numpy array sub-classes.
I agree it will be nice. This particular case will be tricky -- the view limit object is created before and without any knowledge of the input data datatype...
Perhaps not directly related but this issue was the closest I could find:
I'd like axvline to accept numpy array as first argument, along these lines:
In [2]: import numpy as np
In [3]: x = np.linspace(-10, 10); y = np.sin(x)
In [4]: plt.plot(x, y); plt.axvline(x); plt.show()
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
now I have to resort to:
In [6]: for _x in x:
...: plt.axvline(_x)
...:
when x is very long I set alpha to e.g. 0.1 (x is not equidistant in that case of course).
Unfortunately this is really slow in matplotlib (when x is a few thousand elements running times
are counted in minutes). Are there any opportunities to make this faster? (or maybe I am approaching this the wrong way?)
axhline/axvline with AstroPy quantities works with astropy/astropy#7037, and I suspect lots of other stuff will start working if that pull request is accepted too.
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Closing as the original issue was fixed downstream. There are plenty of areas where units support could be improved in similar ways (and the data prototype work plays into that), but this particular report is no longer a problem, I think
Activity
mdboom commented on Jan 25, 2016
I agree it will be nice. This particular case will be tricky -- the view limit object is created before and without any knowledge of the input data datatype...
bjodah commented on Jan 19, 2017
Perhaps not directly related but this issue was the closest I could find:
I'd like axvline to accept numpy array as first argument, along these lines:
now I have to resort to:
when x is very long I set alpha to e.g. 0.1 (x is not equidistant in that case of course).
Unfortunately this is really slow in matplotlib (when x is a few thousand elements running times
are counted in minutes). Are there any opportunities to make this faster? (or maybe I am approaching this the wrong way?)
tacaswell commented on Jan 19, 2017
@bjodah I think you are looking for the ax.vlines method http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.vlines.html#matplotlib.axes.Axes.vlines
bjodah commented on Jan 19, 2017
@tacaswell well kind of, but using ymin/ymax: (-inf, inf) does not work:
>>> import matplotlib.pyplot as plt; import numpy as np; >>> x = np.linspace(-5, 5); y = np.sin(x); plt.vlines(x, float('-inf'), float('inf')); plt.plot(x, y); plt.show()(plot shows no vertical lines)
then I need to compute ymin, ymax from the maximum/minimum values of all series which will be plotted later, or use a priori information:
(edit: axvline guarantees to draw the lines across the axes, vlines affects ylim which is not what I want for this particular case).
tacaswell commented on Jan 19, 2017
Use it in conjunction with a blended transform: http://matplotlib.org/users/transforms_tutorial.html#blended-transformations and go from 0 to 1 in axes space.
bjodah commented on Jan 19, 2017
@tacaswell thank you, very neat! For future visitors from e.g. google:
... and performance is much better!
tacaswell commented on Jan 19, 2017
that is probably worth an example in the docs. I have seen this question come across multiple times in multiple places.
dstansby commented on Dec 27, 2017
axhline/axvline with AstroPy quantities works with astropy/astropy#7037, and I suspect lots of other stuff will start working if that pull request is accepted too.
github-actions commented on Mar 21, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
ksunden commented on Mar 21, 2023
Closing as the original issue was fixed downstream. There are plenty of areas where units support could be improved in similar ways (and the data prototype work plays into that), but this particular report is no longer a problem, I think