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

Skip to content

Fix axh{line,span} on polar axes. #26788

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

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/behavior/26788-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``axvspan`` and ``axhspan`` now return ``Rectangle``\s, not ``Polygons``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This change allows using `~.Axes.axhspan` to draw an annulus on polar axes.

This change also affects other elements built via `~.Axes.axvspan` and
`~.Axes.axhspan`, such as ``Slider.poly``.
5 changes: 5 additions & 0 deletions doc/users/next_whats_new/polar-line-spans.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``axhline`` and ``axhspan`` on polar axes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

... now draw circles and circular arcs (`~.Axes.axhline`) or annuli and wedges
(`~.Axes.axhspan`).
25 changes: 20 additions & 5 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
trans = self.get_yaxis_transform(which='grid')
l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs)
self.add_line(l)
l.get_path()._interpolation_steps = mpl.axis.GRIDLINE_INTERPOLATION_STEPS
if scaley:
self._request_autoscale_view("y")
return l
Expand Down Expand Up @@ -851,6 +852,7 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
trans = self.get_xaxis_transform(which='grid')
l = mlines.Line2D([x, x], [ymin, ymax], transform=trans, **kwargs)
self.add_line(l)
l.get_path()._interpolation_steps = mpl.axis.GRIDLINE_INTERPOLATION_STEPS
if scalex:
self._request_autoscale_view("x")
return l
Expand Down Expand Up @@ -978,10 +980,17 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
(ymin, ymax), = self._process_unit_info([("y", [ymin, ymax])], kwargs)

verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)
p = mpatches.Polygon(verts, **kwargs)
p = mpatches.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, **kwargs)
p.set_transform(self.get_yaxis_transform(which="grid"))
# For Rectangles and non-separable transforms, add_patch can be buggy
# and update the x limits even though it shouldn't do so for an
# yaxis_transformed patch, so undo that update.
Comment on lines +985 to +987
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some cutout in _update_patch_limits for x/y axis transforms; is that something that should be changed instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that Rectangle.get_transform() is not equal to get_xaxis/yaxis_transform, because Rectangle is implemented as a unit rectangle plus a sizing and translation affine transform, which ends up being included (first!) in Rectangle.get_transform. I guess ideally we'd want to check something like (-patch.get_patch_transform() + patch.get_transform()) (the subtraction needs to be "on the left"), but the transform system can't even handle that, so for now I don't think that would be so easy.

ix = self.dataLim.intervalx
mx = self.dataLim.minposx
self.add_patch(p)
self.dataLim.intervalx = ix
self.dataLim.minposx = mx
p.get_path()._interpolation_steps = mpl.axis.GRIDLINE_INTERPOLATION_STEPS
self._request_autoscale_view("y")
return p

Expand Down Expand Up @@ -1034,11 +1043,17 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
self._check_no_units([ymin, ymax], ['ymin', 'ymax'])
(xmin, xmax), = self._process_unit_info([("x", [xmin, xmax])], kwargs)

verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)]
p = mpatches.Polygon(verts, **kwargs)
p = mpatches.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, **kwargs)
p.set_transform(self.get_xaxis_transform(which="grid"))
p.get_path()._interpolation_steps = 100
# For Rectangles and non-separable transforms, add_patch can be buggy
# and update the y limits even though it shouldn't do so for an
# xaxis_transformed patch, so undo that update.
iy = self.dataLim.intervaly.copy()
my = self.dataLim.minposy
self.add_patch(p)
self.dataLim.intervaly = iy
self.dataLim.minposy = my
p.get_path()._interpolation_steps = mpl.axis.GRIDLINE_INTERPOLATION_STEPS
self._request_autoscale_view("x")
return p

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8849,3 +8849,15 @@ def test_xylim_changed_shared():
axs[1].callbacks.connect("ylim_changed", events.append)
axs[0].set(xlim=[1, 3], ylim=[2, 4])
assert events == [axs[1], axs[1]]


@image_comparison(["axhvlinespan_interpolation.png"], style="default")
def test_axhvlinespan_interpolation():
ax = plt.figure().add_subplot(projection="polar")
ax.set_axis_off()
ax.axvline(.1, c="C0")
ax.axvspan(.2, .3, fc="C1")
ax.axvspan(.4, .5, .1, .2, fc="C2")
ax.axhline(1, c="C0", alpha=.5)
ax.axhspan(.8, .9, fc="C1", alpha=.5)
ax.axhspan(.6, .7, .8, .9, fc="C2", alpha=.5)
6 changes: 3 additions & 3 deletions lib/matplotlib/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ def test_nonlinear_containment():
ax.set(xscale="log", ylim=(0, 1))
polygon = ax.axvspan(1, 10)
assert polygon.get_path().contains_point(
ax.transData.transform((5, .5)), ax.transData)
ax.transData.transform((5, .5)), polygon.get_transform())
assert not polygon.get_path().contains_point(
ax.transData.transform((.5, .5)), ax.transData)
ax.transData.transform((.5, .5)), polygon.get_transform())
assert not polygon.get_path().contains_point(
ax.transData.transform((50, .5)), ax.transData)
ax.transData.transform((50, .5)), polygon.get_transform())


@image_comparison(['arrow_contains_point.png'],
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ def intersection(bbox1, bbox2):
y1 = np.minimum(bbox1.ymax, bbox2.ymax)
return Bbox([[x0, y0], [x1, y1]]) if x0 <= x1 and y0 <= y1 else None


_default_minpos = np.array([np.inf, np.inf])


Expand Down Expand Up @@ -1011,6 +1012,10 @@ def minpos(self):
"""
return self._minpos

@minpos.setter
def minpos(self, val):
self._minpos[:] = val

@property
def minposx(self):
"""
Expand All @@ -1022,6 +1027,10 @@ def minposx(self):
"""
return self._minpos[0]

@minposx.setter
def minposx(self, val):
self._minpos[0] = val

@property
def minposy(self):
"""
Expand All @@ -1033,6 +1042,10 @@ def minposy(self):
"""
return self._minpos[1]

@minposy.setter
def minposy(self, val):
self._minpos[1] = val

def get_points(self):
"""
Get the points of the bounding box as an array of the form
Expand Down
12 changes: 4 additions & 8 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None,
Notes
-----
Additional kwargs are passed on to ``self.poly`` which is the
`~matplotlib.patches.Polygon` that draws the slider knob. See the
`.Polygon` documentation for valid property names (``facecolor``,
`~matplotlib.patches.Rectangle` that draws the slider knob. See the
`.Rectangle` documentation for valid property names (``facecolor``,
``edgecolor``, ``alpha``, etc.).
"""
super().__init__(ax, orientation, closedmin, closedmax,
Expand Down Expand Up @@ -577,16 +577,12 @@ def set_val(self, val):
----------
val : float
"""
xy = self.poly.xy
if self.orientation == 'vertical':
xy[1] = .25, val
xy[2] = .75, val
self.poly.set_height(val - self.poly.get_y())
self._handle.set_ydata([val])
else:
xy[2] = val, .75
xy[3] = val, .25
self.poly.set_width(val - self.poly.get_x())
self._handle.set_xdata([val])
self.poly.xy = xy
self.valtext.set_text(self._format(val))
if self.drawon:
self.ax.figure.canvas.draw_idle()
Expand Down