Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: fill_between order matters with pandas index #28505

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

Closed
williambdean opened this issue Jul 3, 2024 · 2 comments
Closed

[Bug]: fill_between order matters with pandas index #28505

williambdean opened this issue Jul 3, 2024 · 2 comments

Comments

@williambdean
Copy link

williambdean commented Jul 3, 2024

Bug summary

The ordering of fill_between and pandas.DataFrame/Series.plot with datetime index differs

Code for reproduction

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()

Actual outcome

order-matters

Expected outcome

Both orders would work the same

Additional information

No response

Operating system

No response

Matplotlib Version

3.8.3
EDIT: 3.9.0 as well

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

@ksunden
Copy link
Member

ksunden commented Jul 3, 2024

I do not think there is anything we can do upstream for this, Pandas is calling clear:

https://github.com/pandas-dev/pandas/blob/dcb5494e511cee9643ce3748d4450a97ed1a7c03/pandas/plotting/_matplotlib/timeseries.py#L144

From the perspective of Matplotlib, we are being asked to clear and are clearing, so working as intended. (Note, I only actually saw the clear line in regards to TimeSeries plots, so it may well be the case that if you do something that is not a timeseries, it will not clear... Did not test to confirm that, though, and this example is a TimeSeries)

Discussion on whether or not pandas should be clearing kind of have to be directed to Pandas at that point, however.

@ksunden ksunden closed this as not planned Won't fix, can't repro, duplicate, stale Jul 3, 2024
@williambdean
Copy link
Author

williambdean commented Jul 3, 2024

I do not think there is anything we can do upstream for this, Pandas is calling clear:

https://github.com/pandas-dev/pandas/blob/dcb5494e511cee9643ce3748d4450a97ed1a7c03/pandas/plotting/_matplotlib/timeseries.py#L144

From the perspective of Matplotlib, we are being asked to clear and are clearing, so working as intended. (Note, I only actually saw the clear line in regards to TimeSeries plots, so it may well be the case that if you do something that is not a timeseries, it will not clear... Did not test to confirm that, though, and this example is a TimeSeries)

Discussion on whether or not pandas should be clearing kind of have to be directed to Pandas at that point, however.

Gotcha. Thanks for looking into it.

Just edited the original message with a larger grid. Seems it is under pandas control. Will raise there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants