8000 Merge pull request #20032 from anntzer/polar-xaxis-trf-datalim · matplotlib/matplotlib@cf31ef0 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf31ef0

Browse files
authored
Merge pull request #20032 from anntzer/polar-xaxis-trf-datalim
axvline()/axvspan() should not update r limits in polar plots.
2 parents 5958725 + 6731f2c commit cf31ef0

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,41 +2289,45 @@ def _update_line_limits(self, line):
22892289
if path.vertices.size == 0:
22902290
return
22912291

2292-
line_trans = line.get_transform()
2292+
line_trf = line.get_transform()
22932293

2294-
if line_trans == self.transData:
2294+
if line_trf == self.transData:
22952295
data_path = path
2296-
2297-
elif any(line_trans.contains_branch_seperately(self.transData)):
2298-
# identify the transform to go from line's coordinates
2299-
# to data coordinates
2300-
trans_to_data = line_trans - self.transData
2301-
2302-
# if transData is affine we can use the cached non-affine component
2303-
# of line's path. (since the non-affine part of line_trans is
2304-
# entirely encapsulated in trans_to_data).
2296+
elif any(line_trf.contains_branch_seperately(self.transData)):
2297+
# Compute the transform from line coordinates to data coordinates.
2298+
trf_to_data = line_trf - self.transData
2299+
# If transData is affine we can use the cached non-affine component
2300+
# of line's path (since the non-affine part of line_trf is
2301+
# entirely encapsulated in trf_to_data).
23052302
if self.transData.is_affine:
23062303
line_trans_path = line._get_transformed_path()
23072304
na_path, _ = line_trans_path.get_transformed_path_and_affine()
2308-
data_path = trans_to_data.transform_path_affine(na_path)
2305+
data_path = trf_to_data.transform_path_affine(na_path)
23092306
else:
2310-
data_path = trans_to_data.transform_path(path)
2307+
data_path = trf_to_data.transform_path(path)
23112308
else:
2312-
# for backwards compatibility we update the dataLim with the
2309+
# For backwards compatibility we update the dataLim with the
23132310
# coordinate range of the given path, even though the coordinate
23142311
# systems are completely different. This may occur in situations
23152312
# such as when ax.transAxes is passed through for absolute
23162313
# positioning.
23172314
data_path = path
23182315

2319-
if data_path.vertices.size > 0:
2320-
updatex, updatey = line_trans.contains_branch_seperately(
2321-
self.transData)
2322-
self.dataLim.update_from_path(data_path,
2323-
self.ignore_existing_data_limits,
2324-
updatex=updatex,
2325-
updatey=updatey)
2326-
self.ignore_existing_data_limits = False
2316+
if not data_path.vertices.size:
2317+
return
2318+
2319+
updatex, updatey = line_trf.contains_branch_seperately(self.transData)
2320+
if self.name != "rectilinear":
2321+
# This block is mostly intended to handle axvline in polar plots,
2322+
# for which updatey would otherwise be True.
2323+
if updatex and line_trf == self.get_yaxis_transform():
2324+
updatex = False
2325+
if updatey and line_trf == self.get_xaxis_transform():
2326+
updatey = False
2327+
self.dataLim.update_from_path(data_path,
2328+
self.ignore_existing_data_limits,
2329+
updatex=updatex, updatey=updatey)
2330+
self.ignore_existing_data_limits = False
23272331

23282332
def add_patch(self, p):
23292333
"""
@@ -2354,17 +2358,19 @@ def _update_patch_limits(self, patch):
23542358
p = patch.get_path()
23552359
vertices = p.vertices if p.codes is None else p.vertices[np.isin(
23562360
p.codes, (mpath.Path.CLOSEPOLY, mpath.Path.STOP), invert=True)]
2357-
if vertices.size > 0:
2358-
xys = patch.get_patch_transform().transform(vertices)
2359-
if patch.get_data_transform() != self.transData:
2360-
patch_to_data = (patch.get_data_transform() -
2361-
self.transData)
2362-
xys = patch_to_data.transform(xys)
2363-
2364-
updatex, updatey = patch.get_transform().\
2365-
contains_branch_seperately(self.transData)
2366-
self.update_datalim(xys, updatex=updatex,
2367-
updatey=updatey)
2361+
if not vertices.size:
2362+
return
2363+
patch_trf = patch.get_transform()
2364+
updatex, updatey = patch_trf.contains_branch_seperately(self.transData)
2365+
if self.name != "rectilinear":
2366+
# As in _update_line_limits, but for axvspan.
2367+
if updatex and patch_trf == self.get_yaxis_transform():
2368+
updatex = False
2369+
if updatey and patch_trf == self.get_xaxis_transform():
2370+
updatey = False
2371+
trf_to_data = patch_trf - self.transData
2372+
xys = trf_to_data.transform(vertices)
2373+
self.update_datalim(xys, updatex=updatex, updatey=updatey)
23682374

23692375
def add_table(self, tab):
23702376
"""

lib/matplotlib/tests/test_polar.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,11 @@ def test_shared_polar_keeps_ticklabels():
402402
fig.canvas.draw()
403403
assert axs[0, 1].xaxis.majorTicks[0].get_visible()
404404
assert axs[0, 1].yaxis.majorTicks[0].get_visible()
405+
406+
407+
def test_axvline_axvspan_do_not_modify_rlims():
408+
ax = plt.subplot(projection="polar")
409+
ax.axvspan(0, 1)
410+
ax.axvline(.5)
411+
ax.plot([.1, .2])
412+
assert ax.get_ylim() == (0, .2)

0 commit comments

Comments
 (0)
0