8000 [Bug]: `ax.stairs()` creates inaccurate `fill` for the plot · Issue #26752 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: ax.stairs() creates inaccurate fill for the plot  #26752
Closed
@pawjast

Description

@pawjast

Bug summary

I was working on a code to achieve the following look:

image

I got there by combining ax.plot() and ax.fill_between() functions. But then I've learnt about the ax.stairs() function which is suppose to plot in a step-like fashion and allows to add fill under the line in one go.

However, when I tried ax.stairs() I got the following result:

image

The bug (I think):

  • the fill is completely wrong - it doesn't stretch between x-axis and the line

Secondary issues:

  • I don't think there's a way to have a separate colour for line and fill at the moment, when I set a colour, line disappears - this is especially visibile when I set e.g. alpha=0.1
  • I've got a feeling that somehow the grid ends up on top of the plot when I call ax.stairs()

Code for reproduction

python
import matplotlib.pyplot as plt

x = [1, 4, 10, 12, 15, 20]
y = [2, 4, 1, 3, 1, 4]

fig, axs = plt.subplots(
    2, 1,
    figsize=(16,10),
    facecolor="white",
    sharex=True,
    sharey=True
)
fig.suptitle(
    "ax.stairs()",
    fontsize=20,
)

# plot points
for ax in axs:
    ax.scatter(
        x, y,
        marker="X",
        s=275,
        linewidth=1.5,
        edgecolor="black",
        facecolor="white",
        zorder=100  # crude way to keep the points always on top
    )

axs[0].stairs(
    values=y,  # step height
    edges=[x[0], *x],  # edge width
    baseline=None,
    color="red",
    fill=True,
    label="steps-pre: duplicate 1st element in `edges=[x[0], *x]`"
)
axs[1].stairs(
    values=y,  # step height
    edges=[*x, x[-1]],  # edge
    baseline=None,
    color="green",
    fill=True,
    alpha=0.1,
    label="steps-post: duplicate last element in `edges=[*x, x[-1]]`"
)

# set axis limits
axs[0].set_ylim(0, 5.5)
axs[0].set_yticks(range(1, 6))
axs[0].set_xlim(0, 21)
axs[1].set_xticks(range(1, 21))

# add legend
for ax in axs:
    ax.legend(
        fontsize=16,
        loc="upper center",
        facecolor="whitesmoke",
        edgecolor="none"
    )

    # Update font size of the 'x' and 'y' axis labels
    ax.tick_params(
        axis="both",
        labelsize=16
    )

    ax.grid(
        color="gainsboro",
        linewidth=0.5
    )

# Remove spines
for position in ["top", "right"]:
    for ax in axs:
        ax.spines[position].set_visible(False)

plt.tight_layout()


### Actual outcome

![image](https://github.com/matplotlib/matplotlib/assets/8880777/f726f74e-5d28-488d-bf66-6536093b3227)


### Expected outcome

![image](https://github.com/matplotlib/matplotlib/assets/8880777/88a1c5e4-f50f-4b30-8f41-59f69aeee169)


### Additional information

_No response_

### Operating system

Windows 

### Matplotlib Version

3.7.2

### Matplotlib Backend

module://matplotlib_inline.backend_inline

### Python version

3.10.2

### Jupyter version

IPython          : 8.14.0 ipykernel        : 6.25.1 ipywidgets       : not installed jupyter_client   : 8.3.0 jupyter_core     : 5.3.1 jupyter_server   : not installed jupyterlab       : not installed nbclient         : not installed nbconvert        : not installed nbformat         : not installed notebook         : not installed qtconsole        : not installed traitlets        : 5.9.0

### Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0