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

Skip to content

Commit 0541220

Browse files
committed
Fix axes vlines and hlines using wrong coordinates
1 parent 70e0ba8 commit 0541220

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,21 +1093,31 @@ 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+
if 'transform' in kwargs:
1097+
lines.set_transform(kwargs['transform'])
10961098
self.add_collection(lines, autolim=False)
1099+
self._unstale_viewLim()
10971100
lines._internal_update(kwargs)
10981101

10991102
if len(y) > 0:
11001103
# Extreme values of xmin/xmax/y. Using masked_verts here handles
11011104
# the case of y being a masked *object* array (as can be generated
11021105
# 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])
1106+
if self.name == "rectilinear":
1107+
datalim = lines.get_datalim(self.transData)
1108+
minx = np.nanmin(datalim.xmin)
1109+
maxx = np.nanmax(datalim.xmax)
1110+
miny = np.nanmin(datalim.ymin)
1111+
maxy = np.nanmax(datalim.ymax)
1112+
else:
1113+
minx = np.nanmin(masked_verts[..., 0])
1114+
maxx = np.nanmax(masked_verts[..., 0])
1115+
miny = np.nanmin(masked_verts[..., 1])
1116+
maxy = np.nanmax(masked_verts[..., 1])
1117+
11071118
corners = (minx, miny), (maxx, maxy)
11081119
self.update_datalim(corners)
11091120
self._request_autoscale_view()
1110-
11111121
return lines
11121122

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

11741184
lines = mcoll.LineCollection(masked_verts, colors=colors,
11751185
linestyles=linestyles, label=label)
1186+
if 'transform' in kwargs:
1187+
lines.set_transform(kwargs['transform'])
11761188
self.add_collection(lines, autolim=False)
1189+
self._unstale_viewLim()
11771190
lines._internal_update(kwargs)
11781191

11791192
if len(x) > 0:
11801193
# Extreme values of x/ymin/ymax. Using masked_verts here handles
11811194
# the case of x being a masked *object* array (as can be generated
11821195
# 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])
1196+
if self.name == "rectilinear":
1197+
datalim = lines.get_datalim(self.transData)
1198+
minx = np.nanmin(datalim.xmin)
1199+
maxx = np.nanmax(datalim.xmax)
1200+
miny = np.nanmin(datalim.ymin)
1201+
maxy = np.nanmax(datalim.ymax)
1202+
else:
1203+
minx = np.nanmin(masked_verts[..., 0])
1204+
maxx = np.nanmax(masked_verts[..., 0])
1205+
miny = np.nanmin(masked_verts[..., 1])
1206+
maxy = np.nanmax(masked_verts[..., 1])
1207+
11871208
corners = (minx, miny), (maxx, maxy)
11881209
self.update_datalim(corners)
11891210
self._request_autoscale_view()
1190-
11911211
return lines
11921212

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

lib/matplotlib/tests/test_axes.py

Lines changed: 22 additions & 0 deletions
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)