Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Open
Description
Bug summary
The documentation for matplotlib.units states:
The classes here provide support for using custom classes with Matplotlib, e.g., those that do not expose the array interface but know how to convert themselves to arrays. It also supports classes with units and units conversion.
without defining the meaning of, and implication of, "array interface". Ultimately, and fundamentally the problem here is that plotting functions like matplotlib.pyplot.plot() cast the x and y data to numpy.ndarray() immediately without consulting the matplotlib.units.registry first and this means that custom classes that are List-like with values that are Number-like are immediately converted to numpy.ndarray and the conversion interface is never called.
Code for reproduction
importmatplotlib.unitsasmpl_unitsclassQuantity(float):"""simple quantity"""classQlist(list):"""a list of Quantity objects"""classQlistConverter(mpl_units.ConversionInterface):@staticmethoddefconvert(obj,unit,axis):print("QlistConverter.convert()")returnobj@staticmethoddefaxisinfo(unit,axis):print("QlistConverter.axisinfo()")returnmpl_units.AxisInfo()@staticmethoddefdefault_units(x,axis):print("QlistConverter.default_units()")returnximportmatplotlib.pyplotaspltx=Qlist([Quantity(1),Quantity(2),Quantity(3)])y=Qlist([Quantity(4),Quantity(5),Quantity(6)])mpl_units.registry[Qlist]=QlistConverter()plt.plot(x,y)plt.gca().xaxis.converterisNone
Actual outcome
True
Expected outcome
Expected the QlistConverter to be called
Additional information
No response
Operating system
No response
Matplotlib Version
3.5.3
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
No response