Skip to content

Better deal with Numpy array subclasses in methods other than plot and scatter #5896

@astrofrog

Description

@astrofrog

The plot and scatter methods now deal nicely with Numpy array sub-classes, such as Astropy quantities (which have attached units):

In [1]: from astropy import units as u

In [2]: import matplotlib.pyplot as plt

In [3]: fig = plt.figure()

In [4]: ax = fig.add_subplot(1,1,1)

In [5]: x = [1,2] * u.m

In [6]: y = [3,4] * u.m

In [7]: ax.plot(x,y)
Out[7]: [<matplotlib.lines.Line2D at 0x107f00048>]

In [8]: ax.scatter(x, y)
Out[8]: <matplotlib.collections.PathCollection at 0x107f00ba8>

However, other methods such as axhline and axvline do not:

In [9]: z = 4 * u.m

In [10]: ax.axhline(z)
---------------------------------------------------------------------------
UnitsError                                Traceback (most recent call last)
<ipython-input-10-39d10b51e434> in <module>()
----> 1 ax.axhline(z)

/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/matplotlib/axes/_axes.py in axhline(self, y, xmin, xmax, **kwargs)
    744         self._process_unit_info(ydata=y, kwargs=kwargs)
    745         yy = self.convert_yunits(y)
--> 746         scaley = (yy < ymin) or (yy > ymax)
    747 
    748         trans = self.get_yaxis_transform(which='grid')

/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/astropy/units/quantity.py in __array_prepare__(self, obj, context)
    344                                      "argument is not a quantity (unless the "
    345                                      "latter is all zero/infinity/nan)"
--> 346                                      .format(function.__name__))
    347             except TypeError:
    348                 # _can_have_arbitrary_unit failed: arg could not be compared

UnitsError: Can only apply 'less' function to dimensionless quantities when other argument is not a quantity (unless the latter is all zero/infinity/nan)

It would be nice to try and make these methods also support Numpy array sub-classes.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy