Skip to content

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

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
dihm opened this issue Apr 14, 2023 · 3 comments · Fixed by #25712
Closed

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

dihm opened this issue Apr 14, 2023 · 3 comments · Fixed by #25712
Milestone

Comments

@dihm
Copy link
Contributor

dihm commented Apr 14, 2023

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

@ksunden
Copy link
Member

ksunden commented Apr 14, 2023

self.update_datalim(pts, updatex=True, updatey=True)

So the fundamental thing here is that transform is not actually directly inspected in fill_between's implementation (which is shared for x/y variants), instead that falls under the **kwargs that are passed straight to Polygon

But it does call self.update_datalim (in the linked line above)

I think when you are doing things in the (default) data axes, you do want to expand the lims.
But yes, when using axes limits (for either x or y) it should likely not.

One way to do that would be to manipulate the update[xy] parameters (they are passed explicitly despite being set to defaults and never passed in other similar calls). But not necessarily clear when to do so.

Alternatively it could be treated like axline, which I think would be my leaning at the moment:

if "transform" in kwargs:
# if a transform is passed (i.e. line points not in data space),
# data limits should not be adjusted.
datalim = []

Ref #17781, #17586

@tacaswell
Copy link
Member

Alternatively it could be treated like axline, which I think would be my leaning at the moment:

That is probably the better option.

olinjohnson added a commit to olinjohnson/matplotlib that referenced this issue Apr 18, 2023
@olinjohnson
Copy link
Contributor

Hey, I'm looking to be a new contributor to the project. Like @ksunden suggested, it seems like amending

            self.update_datalim(pts, updatex=True, updatey=True)    

to

        if "transform" not in kwargs:
            self.update_datalim(pts, updatex=True, updatey=True)

in _fill_between_x_or_y() would work pretty well as a fix.

I'm going to open a pull request - does this seem like a reasonable solution?

@QuLogic QuLogic added this to the v3.8.0 milestone Apr 28, 2023
eslothower pushed a commit to eslothower/matplotlib that referenced this issue May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants