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

Skip to content

fill_between, step='post' and NaN #19444

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
mankoff opened this issue Feb 3, 2021 · 5 comments
Closed

fill_between, step='post' and NaN #19444

mankoff opened this issue Feb 3, 2021 · 5 comments
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action

Comments

@mankoff
Copy link

mankoff commented Feb 3, 2021

Off-by-one error if using fill_between, step='post'

import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure(1)
ax = fig.add_subplot(111)

df = pd.DataFrame(index=pd.date_range(start='2000-01-01', periods=10, freq='1D'))
df['data'] = [1,2,3,-4,-5,-6,7,8,9,-10]

df.plot(ax=ax, drawstyle='steps-post')
ax.fill_between(df[df > 0].index, df[df > 0].values.flatten(), step='post')

Actual outcome

image

Expected outcome

Some of the df[df > 0] columns are not filled.

Matplotlib version

  • Operating system: Linux Kubuntu 20.04
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.3.1
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.8.5
  • Jupyter version (if applicable): IPython 7.17.0
@jklymak
Copy link
Member

jklymak commented Feb 3, 2021

If possible can you simplify this to Matplotlib with no dates, no pandas, and maybe the latest release? I think fill-between may have had some work recently. Thanks!

@jklymak
Copy link
Member

jklymak commented Feb 3, 2021

For instance if I do:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(1)
ax = fig.add_subplot(111)
x = np.arange(30)
np.random.seed(1)
y = np.cumsum(np.random.randn(30))
y = y - np.mean(y)
ax.plot(x, y, drawstyle='steps-post')
yy = y
yy[y<0] = 0
ax.fill_between(x, yy, step='post')
plt.show()

I get what I think you are trying to do, so I'm guessing this is actually to do with how you are using pandas (but I could certainly be wrong):

TestFill

@mankoff
Copy link
Author

mankoff commented Feb 3, 2021

Yes this works with the extra code you've provided: yy = y; yy[y<0] = 0.

Thanks for the push to simplify the bug. It is an NaN bug, and I think this should work with NaNs, although replacing NaNs with 0 is not a big burden.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(1)
ax = fig.add_subplot(111)

x = np.arange(1,11)
y = np.array([1,2,3,-4,-5,-6,7,8,9,-10])
yn = np.where(y > 0, y, np.nan)

ax.plot(x, y, drawstyle='steps-post')
ax.fill_between(x, yn, step='post')
plt.show()

The above code generates the first graphic: https://user-images.githubusercontent.com/145117/106748570-46cc4480-65da-11eb-8e38-83e2d4e02fc2.png

@mankoff mankoff changed the title fill_between and step='post' off-by-one (off by 1) fill_between, step='post' and NaN Feb 3, 2021
@mankoff
Copy link
Author

mankoff commented Feb 8, 2021

See related: #15065 and #16635

@github-actions
Copy link

github-actions bot commented Oct 4, 2023

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Oct 4, 2023
@github-actions github-actions bot added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label Nov 15, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action
Projects
None yet
Development

No branches or pull requests

2 participants