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

Skip to content

Commit 3316180

Browse files
henryhu123DennisTismenko
authored andcommitted
Fix #13799
1 parent 7bae46c commit 3316180

File tree

2 files changed

+92
-16
lines changed

2 files changed

+92
-16
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,15 +1124,17 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
11241124
if not np.iterable(xmax):
11251125
xmax = [xmax]
11261126

1127-
y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax)
1128-
1127+
y, xmin, xmax = cbook._combine_masks(y, xmin, xmax)
11291128
y = np.ravel(y)
1130-
xmin = np.resize(xmin, y.shape)
1131-
xmax = np.resize(xmax, y.shape)
1132-
1133-
verts = [((thisxmin, thisy), (thisxmax, thisy))
1134-
for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)]
1135-
lines = mcoll.LineCollection(verts, colors=colors,
1129+
xmin = np.ma.resize(xmin, y.shape)
1130+
xmax = np.ma.resize(xmax, y.shape)
1131+
1132+
# Keep all the invalid points instead of deleting them,
1133+
# convert verts to MaskedArry.
1134+
masked_verts = [np.ma.array(
1135+
[y_xmin, y_xmax])for y_xmin, y_xmax in zip(
1136+
np.ma.array([xmin, y]).T, np.ma.array([xmax, y]).T)]
1137+
lines = mcoll.LineCollection(masked_verts, colors=colors,
11361138
linestyles=linestyles, label=label)
11371139
self.add_collection(lines, autolim=False)
11381140
lines.update(kwargs)
@@ -1202,15 +1204,17 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
12021204
if not np.iterable(ymax):
12031205
ymax = [ymax]
12041206

1205-
x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax)
1206-
1207+
x, ymin, ymax = cbook._combine_masks(x, ymin, ymax)
12071208
x = np.ravel(x)
1208-
ymin = np.resize(ymin, x.shape)
1209-
ymax = np.resize(ymax, x.shape)
1210-
1211-
verts = [((thisx, thisymin), (thisx, thisymax))
1212-
for thisx, thisymin, thisymax in zip(x, ymin, ymax)]
1213-
lines = mcoll.LineCollection(verts, colors=colors,
1209+
ymin = np.ma.resize(ymin, x.shape)
1210+
ymax = np.ma.resize(ymax, x.shape)
1211+
1212+
# Keep all the invalid points instead of deleting them
1213+
# convert verts to MaskedArry.
1214+
masked_verts = [np.ma.array(
1215+
[xymin, xymax])for xymin, xymax in zip(
1216+
np.ma.array([x, ymin]).T, np.ma.array([x, ymax]).T)]
1217+
lines = mcoll.LineCollection(masked_verts, colors=colors,
12141218
linestyles=linestyles, label=label)
12151219
self.add_collection(lines, autolim=False)
12161220
lines.update(kwargs)

lib/matplotlib/tests/test_axes.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,6 +3992,78 @@ def test_hlines():
39923992
ax5.set_ylim(0, 15)
39933993

39943994

3995+
@check_figures_equal()
3996+
def test_vlines_with_nan_colors(fig_test, fig_ref):
3997+
colors1 = ['red', 'green', 'blue', 'purple', 'orange', 'black']
3998+
x1 = [2, 3, 4, 5, 6, 7]
3999+
y1 = [2, -6, 3, 8, np.nan, 2]
4000+
4001+
fig_test, ax1 = plt.subplots()
4002+
ax1.vlines(x1, 0, y1, colors=colors1, linewidth=5)
4003+
4004+
colors2 = ['red', 'green', 'blue', 'purple', 'black']
4005+
x2 = [2, 3, 4, 5, 7]
4006+
y2 = [2, -6, 3, 8, 2]
4007+
# Reference image
4008+
fig_ref, ax2 = plt.subplots()
4009+
ax2.vlines(x2, 0, y2, colors=colors2, linewidth=5)
4010+
4011+
4012+
@check_figures_equal()
4013+
def test_vlines_with_masked_colors(fig_test, fig_ref):
4014+
colors1 = ['red', 'green', 'blue', 'purple', 'orange', 'black']
4015+
fig_test, ax1 = plt.subplots()
4016+
x1 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8)
4017+
ymin1 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2)
4018+
ymax1 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18)
4019+
ax1.vlines(x1, ymin1, ymax1, colors=colors1, linewidth=2)
4020+
ax1.set_xlim(0, 15)
4021+
4022+
colors2 = ['red', 'green', 'blue']
4023+
fig_ref, ax2 = plt.subplots()
4024+
x2 = np.asarray([2, 4, 6])
4025+
ymin2 = np.asarray([0, 1, -1])
4026+
ymax2 = np.asarray([13, 14, 15])
4027+
ax2.vlines(x2, ymin2, ymax2, colors=colors2, linewidth=2)
4028+
ax2.set_xlim(0, 15)
4029+
4030+
4031+
@check_figures_equal()
4032+
def test_hlines_with_nan_colors(fig_test, fig_ref):
4033+
colors1 = ['red', 'green', 'blue', 'purple', 'orange', 'black']
4034+
x1 = [2, 3, 4, 5, 6, 7]
4035+
y1 = [2, -6, 3, 8, np.nan, 2]
4036+
4037+
fig_test, ax1 = plt.subplots()
4038+
ax1.vlines(y1, 0, x1, colors=colors1, linewidth=5)
4039+
4040+
colors2 = ['red', 'green', 'blue', 'purple', 'black']
4041+
x2 = [2, 3, 4, 5, 7]
4042+
y2 = [2, -6, 3, 8, 2]
4043+
# Reference image
4044+
fig_ref, ax2 = plt.subplots()
4045+
ax2.vlines(y2, 0, x2, colors=colors2, linewidth=5)
4046+
4047+
4048+
@check_figures_equal()
4049+
def test_hlines_with_masked_colors(fig_test, fig_ref):
4050+
colors1 = ['red', 'green', 'blue', 'purple', 'orange', 'black']
4051+
fig_test, ax1 = plt.subplots()
4052+
y1 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8)
4053+
xmin1 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2)
4054+
xmax1 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18)
4055+
ax1.vlines(y1, xmin1, xmax1, colors=colors1, linewidth=2)
4056+
ax1.set_xlim(0, 15)
4057+
4058+
colors2 = ['red', 'green', 'blue']
4059+
fig_ref, ax2 = plt.subplots()
4060+
y2 = np.asarray([2, 4, 6])
4061+
xmin2 = np.asarray([0, 1, -1])
4062+
xmax2 = np.asarray([13, 14, 15])
4063+
ax2.vlines(y2, xmin2, xmax2, colors=colors2, linewidth=2)
4064+
ax2.set_xlim(0, 15)
4065+
4066+
39954067
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True)
39964068
def test_step_linestyle():
39974069
x = y = np.arange(10)

0 commit comments

Comments
 (0)