Skip to content

Commit 382be60

Browse files
Fixed incorrect colour in ErrorBar when Nan value is presented (#16724)
* Resolve inconsistent NaN handling behaviour Combine array masks rather than deleting masked points to maintain consistency across the project. Add appropriate test cases for validating color correctness for hlines and vlines. Fixes issue #13799. * Fix test cases based on feedback * Improved test cases * Refactor masked vertices creation * Refactor hlines and vlines Co-authored-by: Dennis Tismenko <dtismenkodeveloper@gmail.com>
1 parent e92685a commit 382be60

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

lib/matplotlib/axes/_axes.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -1123,15 +1123,19 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
11231123
if not np.iterable(xmax):
11241124
xmax = [xmax]
11251125

1126-
y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax)
1127-
1126+
# Create and combine masked_arrays from input
1127+
y, xmin, xmax = cbook._combine_masks(y, xmin, xmax)
11281128
y = np.ravel(y)
1129-
xmin = np.resize(xmin, y.shape)
1130-
xmax = np.resize(xmax, y.shape)
1129+
xmin = np.ravel(xmin)
1130+
xmax = np.ravel(xmax)
1131+
1132+
masked_verts = np.ma.empty((len(y), 2, 2))
1133+
masked_verts[:, 0, 0] = xmin
1134+
masked_verts[:, 0, 1] = y
1135+
masked_verts[:, 1, 0] = xmax
1136+
masked_verts[:, 1, 1] = y
11311137

1132-
verts = [((thisxmin, thisy), (thisxmax, thisy))
1133-
for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)]
1134-
lines = mcoll.LineCollection(verts, colors=colors,
1138+
lines = mcoll.LineCollection(masked_verts, colors=colors,
11351139
linestyles=linestyles, label=label)
11361140
self.add_collection(lines, autolim=False)
11371141
lines.update(kwargs)
@@ -1201,15 +1205,19 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
12011205
if not np.iterable(ymax):
12021206
ymax = [ymax]
12031207

1204-
x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax)
1205-
1208+
# Create and combine masked_arrays from input
1209+
x, ymin, ymax = cbook._combine_masks(x, ymin, ymax)
12061210
x = np.ravel(x)
1207-
ymin = np.resize(ymin, x.shape)
1208-
ymax = np.resize(ymax, x.shape)
1211+
ymin = np.ravel(ymin)
1212+
ymax = np.ravel(ymax)
1213+
1214+
masked_verts = np.ma.empty((len(x), 2, 2))
1215+
masked_verts[:, 0, 0] = x
1216+
masked_verts[:, 0, 1] = ymin
1217+
masked_verts[:, 1, 0] = x
1218+
masked_verts[:, 1, 1] = ymax
12091219

1210-
verts = [((thisx, thisymin), (thisx, thisymax))
1211-
for thisx, thisymin, thisymax in zip(x, ymin, ymax)]
1212-
lines = mcoll.LineCollection(verts, colors=colors,
1220+
lines = mcoll.LineCollection(masked_verts, colors=colors,
12131221
linestyles=linestyles, label=label)
12141222
self.add_collection(lines, autolim=False)
12151223
lines.update(kwargs)

lib/matplotlib/tests/test_axes.py

+18
Original file line numberDiff line numberDiff line change
@@ -3997,6 +3997,24 @@ def test_hlines():
39973997
ax5.set_ylim(0, 15)
39983998

39993999

4000+
@pytest.mark.parametrize('data', [[1, 2, 3, np.nan, 5],
4001+
np.ma.masked_equal([1, 2, 3, 4, 5], 4)])
4002+
@check_figures_equal(extensions=["png"])
4003+
def test_lines_with_colors(fig_test, fig_ref, data):
4004+
test_colors = ['red', 'green', 'blue', 'purple', 'orange']
4005+
fig_test.add_subplot(2, 1, 1).vlines(data, 0, 1,
4006+
colors=test_colors, linewidth=5)
4007+
fig_test.add_subplot(2, 1, 2).hlines(data, 0, 1,
4008+
colors=test_colors, linewidth=5)
4009+
4010+
expect_xy = [1, 2, 3, 5]
4011+
expect_color = ['red', 'green', 'blue', 'orange']
4012+
fig_ref.add_subplot(2, 1, 1).vlines(expect_xy, 0, 1,
4013+
colors=expect_color, linewidth=5)
4014+
fig_ref.add_subplot(2, 1, 2).hlines(expect_xy, 0, 1,
4015+
colors=expect_color, linewidth=5)
4016+
4017+
40004018
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True)
40014019
def test_step_linestyle():
40024020
x = y = np.arange(10)

0 commit comments

Comments
 (0)