Skip to content

Commit 217fc0d

Browse files
committed
Fix axes vlines and hlines using wrong coordinates
1 parent 5937318 commit 217fc0d

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

lib/matplotlib/axes/_axes.py

+40-12
Original file line numberDiff line numberDiff line change
@@ -1094,21 +1094,35 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
10941094

10951095
lines = mcoll.LineCollection(masked_verts, colors=colors,
10961096
linestyles=linestyles, label=label)
1097+
if 'transform' in kwargs:
1098+
lines.set_transform(kwargs['transform'])
10971099
self.add_collection(lines, autolim=False)
1100+
self._unstale_viewLim()
10981101
lines._internal_update(kwargs)
10991102

11001103
if len(y) > 0:
11011104
# Extreme values of xmin/xmax/y. Using masked_verts here handles
11021105
# the case of y being a masked *object* array (as can be generated
11031106
# e.g. by errorbar()), which would make nanmin/nanmax stumble.
1104-
minx = np.nanmin(masked_verts[..., 0])
1105-
maxx = np.nanmax(masked_verts[..., 0])
1106-
miny = np.nanmin(masked_verts[..., 1])
1107-
maxy = np.nanmax(masked_verts[..., 1])
1107+
updatex = True
1108+
updatey = True
1109+
if self.name == "rectilinear":
1110+
datalim = lines.get_datalim(self.transData)
1111+
t = lines.get_transform()
1112+
updatex, updatey = t.contains_branch_seperately(self.transData)
1113+
minx = np.nanmin(datalim.xmin)
1114+
maxx = np.nanmax(datalim.xmax)
1115+
miny = np.nanmin(datalim.ymin)
1116+
maxy = np.nanmax(datalim.ymax)
1117+
else:
1118+
minx = np.nanmin(masked_verts[..., 0])
1119+
maxx = np.nanmax(masked_verts[..., 0])
1120+
miny = np.nanmin(masked_verts[..., 1])
1121+
maxy = np.nanmax(masked_verts[..., 1])
1122+
11081123
corners = (minx, miny), (maxx, maxy)
1109-
self.update_datalim(corners)
1124+
self.update_datalim(corners, updatex, updatey)
11101125
self._request_autoscale_view()
1111-
11121126
return lines
11131127

11141128
@_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"],
@@ -1174,21 +1188,35 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
11741188

11751189
lines = mcoll.LineCollection(masked_verts, colors=colors,
11761190
linestyles=linestyles, label=label)
1191+
if 'transform' in kwargs:
1192+
lines.set_transform(kwargs['transform'])
11771193
self.add_collection(lines, autolim=False)
1194+
self._unstale_viewLim()
11781195
lines._internal_update(kwargs)
11791196

11801197
if len(x) > 0:
11811198
# Extreme values of x/ymin/ymax. Using masked_verts here handles
11821199
# the case of x being a masked *object* array (as can be generated
11831200
# e.g. by errorbar()), which would make nanmin/nanmax stumble.
1184-
minx = np.nanmin(masked_verts[..., 0])
1185-
maxx = np.nanmax(masked_verts[..., 0])
1186-
miny = np.nanmin(masked_verts[..., 1])
1187-
maxy = np.nanmax(masked_verts[..., 1])
1201+
updatex = True
1202+
updatey = True
1203+
if self.name == "rectilinear":
1204+
datalim = lines.get_datalim(self.transData)
1205+
t = lines.get_transform()
1206+
updatex, updatey = t.contains_branch_seperately(self.transData)
1207+
minx = np.nanmin(datalim.xmin)
1208+
maxx = np.nanmax(datalim.xmax)
1209+
miny = np.nanmin(datalim.ymin)
1210+
maxy = np.nanmax(datalim.ymax)
1211+
else:
1212+
minx = np.nanmin(masked_verts[..., 0])
1213+
maxx = np.nanmax(masked_verts[..., 0])
1214+
miny = np.nanmin(masked_verts[..., 1])
1215+
maxy = np.nanmax(masked_verts[..., 1])
1216+
11881217
corners = (minx, miny), (maxx, maxy)
1189-
self.update_datalim(corners)
1218+
self.update_datalim(corners, updatex, updatey)
11901219
self._request_autoscale_view()
1191-
11921220
return lines
11931221

11941222
@_preprocess_data(replace_names=["positions", "lineoffsets",

lib/matplotlib/tests/test_axes.py

+14
Original file line numberDiff line numberDiff line change
@@ -4920,6 +4920,20 @@ def test_lines_with_colors(fig_test, fig_ref, data):
49204920
colors=expect_color, linewidth=5)
49214921

49224922

4923+
@image_comparison(['vlines_hlines_blended_transform'],
4924+
extensions=['png'], style='mpl20')
4925+
def test_vlines_hlines_blended_transform():
4926+
t = np.arange(5.0, 10.0, 0.1)
4927+
s = np.exp(-t) + np.sin(2 * np.pi * t) + 10
4928+
fig, (hax, vax) = plt.subplots(2, 1, figsize=(6, 6))
4929+
hax.plot(t, s, '^')
4930+
hax.hlines([10, 9], xmin=0, xmax=0.5,
4931+
transform=hax.get_yaxis_transform(), colors='r')
4932+
vax.plot(t, s, '^')
4933+
vax.vlines([6, 7], ymin=0, ymax=0.15, transform=vax.get_xaxis_transform(),
4934+
colors='r')
4935+
4936+
49234937
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True,
49244938
tol=0.2)
49254939
def test_step_linestyle():

0 commit comments

Comments
 (0)