Skip to content

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

Closed

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

Activity

  1. ksunden commented on Apr 14, 2023

    @ksunden
    Member

    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

  2. tacaswell commented on Apr 14, 2023

    @tacaswell
    Member

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

    That is probably the better option.

  3. added a commit that references this issue on Apr 18, 2023
  4. olinjohnson commented on Apr 18, 2023

    @olinjohnson
    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?

  5. added this to the v3.8.0 milestone on Apr 28, 2023
  6. added a commit that references this issue on May 3, 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions