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

Skip to content

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

Closed as not planned
Closed as not planned
@williambdean

Description

@williambdean

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions