8000 BUG: plotting with Numpy array subclasses is slow with Matplotlib 3.1.0 (regression) · Issue #14274 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
BUG: plotting with Numpy array subclasses is slow with Matplotlib 3.1.0 (regression) #14274
Closed
@astrofrog

Description

@astrofrog

Bug summary

Astropy implements a ConversionInterface so as to be able to plot quantities with units (represented by the astropy Quantity class, which is a Numpy array subclass). However, this functionality is broken with Matplotlib 3.1 as it looks like Matplotlib now tries to iterate over every element of the array we are trying to plot.

Code for reproduction

import numpy as np
from astropy import units as u
from matplotlib import pyplot as plt
from astropy.visualization.units import quantity_support

values = np.arange(1000000) * u.m

with quantity_support():
    plt.hist(values, bins=50)

plt.savefig('test.png')

Actual outcome

Python hangs.

Expected outcome

This was previously very efficient.

The commit that broke this was 9eac832 (sorry @jklymak!) - and in particular the issue is that Quantity is an instance of collections.abc.Iterable, so the first 'if' statement doesn't get executed and instead Matplotlib iterates over all values. Actually what is strange is that this should also cause slowdown for plain numpy arrays, which are also instances of Iterable, and this might happen, but isn't as noticeable. What is different here is that the Quantity initializer is slow enough that this is noticeable.

We didn't notice this sooner as this was only really tested with large arrays in the docs, which aren't run against Matplotlib dev...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.topic: units and array ducktypes

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0