Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: add_patch incorrectly decides whether to autoscale axes for non-separable transforms #26796

Open
@anntzer

Description

@anntzer

Bug summary

Axes.add_patch(patch) is intended to update the axes limits based on the patch limits only if that can help making the patch fit within the view; in other words, if the patch uses e.g. Axes.get_xaxis_transform() (i.e. is an axvspan), then autoscaling the y (data) limits doesn't do anything to help making the patch fit within the view as the patch's y coordinates are in axes relative units.

However, this patch is incorrectly implemented for non-separable axes.

Code for reproduction

from matplotlib.patches import Rectangle
from matplotlib import pyplot as plt

fig = plt.figure()
axs = [fig.add_subplot(121), fig.add_subplot(122, projection="polar")]
for ax in axs:
    theta0 = 0; dtheta = 1
    r0 = 0; dr = .25
    ax.add_patch(Rectangle((theta0, r0), dtheta, dr, transform=ax.get_xaxis_transform()))
    ax.autoscale_view()
plt.show()

Actual outcome

Figure_1
On the rectilinear axes, add_patch correctly understands that the rectangle uses relative y limits (0, 0.25) and therefore keeps the default y limits (-0.05, 0.05)).
On the polar axes, it doesn't (even though the patch indeed goes to 0.25 the max radius), and thus tries to autoscale the radial axis to (0, 0.25) (+ margin).

Expected outcome

No autoscaling of the y limits on the second plot.

Additional information

I suspect this comes down to understanding/writing down what would be the really useful semantics for contains_branch_separately (upon which add_patch relies) in that case.

(Noted while working on #26788, where I just manually undo the "wrong" axes limits update.)

I suspect that a proper generic solution for #15518 (which was fixed by special-casing in #25324) would require something similar too.

Operating system

macOS

Matplotlib Version

3.8

Matplotlib Backend

qtagg

Python version

3.11

Jupyter version

none

Installation

git checkout

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions