Skip to content

fill_between, step='post' and NaN #19444

Closed as not planned
Closed as not planned

Description

@mankoff

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

Activity

  1. jklymak commented on Feb 3, 2021

    @jklymak
    Member

    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!

  2. jklymak commented on Feb 3, 2021

    @jklymak
    Member

    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

  3. mankoff commented on Feb 3, 2021

    @mankoff
    Author

    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

  4. changed the title [-]fill_between and step='post' off-by-one (off by 1)[/-] [+]fill_between, step='post' and NaN[/+] on Feb 3, 2021
  5. mankoff commented on Feb 8, 2021

    @mankoff
    Author

    See related: #15065 and #16635

  6. github-actions commented on Oct 4, 2023

    @github-actions

    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!

  7. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions