-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: Don't add extra attributes to matplotlib axes #54485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@mroeschke for clarification, do we just stop adding this ? |
Ideally all existing functionality should still work without adding extra attributes to a matplotlib axis, so these attributes still need to be passed along somehow (IMO this is probably a nontrival change) |
I've been looking at this and am currently skeptical we can get rid of all of this ugly pattern. We have some tests that seem to pretty directly rely on storing state in
In the last line here the Some ways that come to mind to avoid this:
|
Disabling the place where we pin "right_ax" and "left_ax" breaks 30 tests. Of those 16 has the test itself directly trying to access one of these attributes. it tentatively looks like many of these have the pattern where we call multiple .plot calls with a re-used |
I believe that this example is related: import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
n_dates = 52 * 3
dates = pd.date_range("2022-01-01", periods=n_dates, freq="W-MON")
seed = sum(map(ord, "Order matters"))
rng = np.random.default_rng(seed)
data = rng.normal(size=n_dates).cumsum()
ser = pd.Series(data, index=dates)
padding = 15
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=False, sharey=True)
def plot_time_series(pandas: bool, ax: plt.Axes):
if pandas:
ser.plot(ax=ax)
else:
ax.plot(dates, data)
def plot_fill_between(ax: plt.Axes):
ax.fill_between(dates, data - padding, data + padding, alpha=0.25)
ax = axes[0, 0]
plot_time_series(pandas=True, ax=ax)
plot_fill_between(ax)
ax.set(title="time-series first", ylabel="pandas.Series.plot")
ax = axes[0, 1]
plot_fill_between(ax)
plot_time_series(pandas=True, ax=ax)
ax.set(title="time-series second")
ax = axes[1, 0]
plot_time_series(pandas=False, ax=ax)
plot_fill_between(ax)
ax.set(title="", ylabel="plt.plot")
ax = axes[1, 1]
plot_fill_between(ax)
plot_time_series(pandas=False, ax=ax)
ax.set(title="")
plt.show() Coming from here: pandas/pandas/plotting/_matplotlib/timeseries.py Lines 141 to 144 in dcb5494
and brought from here: matplotlib/matplotlib#28505 |
Currently there's a few areas where pandas adds extra attributes to a matplotlib axis
pandas/pandas/plotting/_matplotlib/core.py
Line 1425 in a936863
pandas/pandas/plotting/_matplotlib/core.py
Line 487 in a936863
Since axes are stateful and there no way to clear these attributes via matplotlib public APIs, these attributes can cause issues when they are reused (discovered by running tests via pytest-randomly)
The text was updated successfully, but these errors were encountered: