Skip to content

Commit 8980b9c

Browse files
committed
RR - fix axes vlines and hlines using wrong coordinates
1 parent 70e0ba8 commit 8980b9c

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

lib/matplotlib/axes/_axes.py

+31-10
Original file line numberDiff line numberDiff line change
@@ -1093,21 +1093,32 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
10931093

10941094
lines = mcoll.LineCollection(masked_verts, colors=colors,
10951095
linestyles=linestyles, label=label)
1096+
lines._internal_update(kwargs)
1097+
if 'transform' in kwargs:
1098+
lines.set_transform(kwargs['transform'])
10961099
self.add_collection(lines, autolim=False)
1100+
self._unstale_viewLim()
10971101
lines._internal_update(kwargs)
10981102

10991103
if len(y) > 0:
11001104
# Extreme values of xmin/xmax/y. Using masked_verts here handles
11011105
# the case of y being a masked *object* array (as can be generated
11021106
# e.g. by errorbar()), which would make nanmin/nanmax stumble.
1103-
minx = np.nanmin(masked_verts[..., 0])
1104-
maxx = np.nanmax(masked_verts[..., 0])
1105-
miny = np.nanmin(masked_verts[..., 1])
1106-
maxy = np.nanmax(masked_verts[..., 1])
1107+
if self.name == "rectilinear":
1108+
datalim = lines.get_datalim(self.transData)
1109+
minx = np.nanmin(datalim.xmin)
1110+
maxx = np.nanmax(datalim.xmax)
1111+
miny = np.nanmin(datalim.ymin)
1112+
maxy = np.nanmax(datalim.ymax)
1113+
else:
1114+
minx = np.nanmin(masked_verts[..., 0])
1115+
maxx = np.nanmax(masked_verts[..., 0])
1116+
miny = np.nanmin(masked_verts[..., 1])
1117+
maxy = np.nanmax(masked_verts[..., 1])
1118+
11071119
corners = (minx, miny), (maxx, maxy)
11081120
self.update_datalim(corners)
11091121
self._request_autoscale_view()
1110-
11111122
return lines
11121123

11131124
@_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"],
@@ -1173,21 +1184,31 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
11731184

11741185
lines = mcoll.LineCollection(masked_verts, colors=colors,
11751186
linestyles=linestyles, label=label)
1187+
if 'transform' in kwargs:
1188+
lines.set_transform(kwargs['transform'])
11761189
self.add_collection(lines, autolim=False)
1190+
self._unstale_viewLim()
11771191
lines._internal_update(kwargs)
11781192

11791193
if len(x) > 0:
11801194
# Extreme values of x/ymin/ymax. Using masked_verts here handles
11811195
# the case of x being a masked *object* array (as can be generated
11821196
# e.g. by errorbar()), which would make nanmin/nanmax stumble.
1183-
minx = np.nanmin(masked_verts[..., 0])
1184-
maxx = np.nanmax(masked_verts[..., 0])
1185-
miny = np.nanmin(masked_verts[..., 1])
1186-
maxy = np.nanmax(masked_verts[..., 1])
1197+
if self.name == "rectilinear":
1198+
datalim = lines.get_datalim(self.transData)
1199+
minx = np.nanmin(datalim.xmin)
1200+
maxx = np.nanmax(datalim.xmax)
1201+
miny = np.nanmin(datalim.ymin)
1202+
maxy = np.nanmax(datalim.ymax)
1203+
else:
1204+
minx = np.nanmin(masked_verts[..., 0])
1205+
maxx = np.nanmax(masked_verts[..., 0])
1206+
miny = np.nanmin(masked_verts[..., 1])
1207+
maxy = np.nanmax(masked_verts[..., 1])
1208+
11871209
corners = (minx, miny), (maxx, maxy)
11881210
self.update_datalim(corners)
11891211
self._request_autoscale_view()
1190-
11911212
return lines
11921213

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

lib/matplotlib/tests/test_axes.py

+22
Original file line numberDiff line numberDiff line change
@@ -4938,6 +4938,28 @@ def test_lines_with_colors(fig_test, fig_ref, data):
49384938
colors=expect_color, linewidth=5)
49394939

49404940

4941+
@image_comparison(['hlines_blended_transform'],
4942+
extensions=['png'], style='mpl20')
4943+
def test_hlines_blended_transform():
4944+
t = np.arange(5.0, 10.0, 0.1)
4945+
s = np.exp(-t) + np.sin(2 * np.pi * t) + 10
4946+
vax = plt.figure(figsize=(12, 6)).gca()
4947+
vax.plot(t, s, '^')
4948+
vax.hlines([10, 9], xmin=0, xmax=0.5,
4949+
transform=vax.get_yaxis_transform(), colors='r')
4950+
4951+
4952+
@image_comparison(['vlines_blended_transform'],
4953+
extensions=['png'], style='mpl20')
4954+
def test_vlines_blended_transform():
4955+
t = np.arange(5.0, 10.0, 0.1)
4956+
s = np.exp(-t) + np.sin(2 * np.pi * t) + 10
4957+
vax = plt.figure(figsize=(12, 6)).gca()
4958+
vax.plot(t, s, '^')
4959+
vax.vlines([6, 7], ymin=0, ymax=0.15, transform=vax.get_xaxis_transform(),
4960+
colors='r')
4961+
4962+
49414963
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True)
49424964
def test_step_linestyle():
49434965
x = y = np.arange(10)

0 commit comments

Comments
 (0)