Skip to content

[Bug]: fill_between{x} does not respect Axes transform #25682

Closed
@dihm

Description

@dihm

Bug summary

Using an axes transform with fill_between and fill_betweenx incorrectly sets the axes limits if the Axes coordinates are larger than the data coordinates.

Code for reproduction

fig, ax = plt.subplots()
x = np.arange(0, 4 * np.pi, 0.01)
y = 0.1*np.sin(x)
ax.plot(x, y, color='black')

threshold = 0.075
ax.axhline(threshold, color='green', lw=2, alpha=0.7)
ax.fill_between(x, 0, 1, where=y > threshold,
                color='green', alpha=0.5, transform=ax.get_xaxis_transform())

Actual outcome

Note that code is slightly modified from the example in the documentation, but with the y-data values and threshold reduced by a factor of 10. What you get a plot where the y-limits have been expanded as if I've plotted y-data spanning between (0,1), but get a fill that covers the entire axis space.

Expected outcome

Should look like the example in the documentation, but with y axis labels reduced by a factor of 10.

Additional information

My guess is that the y-axis limits are being set by the y1/y2 values in data coordinates before the transform is applied to actually fill the regions. You will get the expected result as long as the provided Axes coordinate values are less than the extreme values of the y-data itself.

For example ax.fill_between(x, 0, 0.1, ...) gives a correct result.

But this issue means that you can't span the axes using this technique if your plotted data does not already span y=(0,1).

Operating system

Windows 10

Matplotlib Version

3.7.1

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.8.16

Jupyter version

3.5.3

Installation

conda

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