8000 Merge pull request #7408 from phobson/vlines-with-nan · matplotlib/matplotlib@ad192f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad192f0

Browse files
committed
Merge pull request #7408 from phobson/vlines-with-nan
[MRG] Handle nan/masked values Axes.vlines and hlines Conflicts: lib/matplotlib/axes/_axes.py We keep the v2.x version on 2 lines: use unpack_labeled_data, instead of _preprocess_data, which is only in master.
1 parent 0e89671 commit ad192f0

File tree

8 files changed

+102
-16
lines changed

8 files changed

+102
-16
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 18 additions & 16 deletions
< 6D40 td data-grid-cell-id="diff-608e2a1bf3d32fd631b0763599a3334d59822c11cb4fec1fa7398c9714592900-1066-1068-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -972,17 +972,18 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
972972
if not iterable(xmax):
973973
xmax = [xmax]
974974

975-
y = np.ravel(y)
975+
y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax)
976976

977+
y = np.ravel(y)
977978
xmin = np.resize(xmin, y.shape)
978979
xmax = np.resize(xmax, y.shape)
979980

980981
verts = [((thisxmin, thisy), (thisxmax, thisy))
981982
for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)]
982-
coll = mcoll.LineCollection(verts, colors=colors,
983-
linestyles=linestyles, label=label)
984-
self.add_collection(coll, autolim=False)
985-
coll.update(kwargs)
983+
lines = mcoll.LineCollection(verts, colors=colors,
984+
linestyles=linestyles, label=label)
985+
self.add_collection(lines, autolim=False)
986+
lines.update(kwargs)
986987

987988
if len(y) > 0:
988989
minx = min(xmin.min(), xmax.min())
@@ -995,7 +996,7 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
995996
self.update_datalim(corners)
996997
self.autoscale_view()
997998

998-
return coll
999+
return lines
9991000

10001001
@unpack_labeled_data(replace_names=["x", "ymin", "ymax", "colors"],
10011002
label_namer="x")
@@ -1053,30 +1054,31 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
10531054
if not iterable(ymax):
10541055
ymax = [ymax]
10551056

1057+
x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax)
1058+
10561059
x = np.ravel(x)
10571060
ymin = np.resize(ymin, x.shape)
10581061
ymax = np.resize(ymax, x.shape)
10591062

10601063
verts = [((thisx, thisymin), (thisx, thisymax))
10611064
for thisx, thisymin, thisymax in zip(x, ymin, ymax)]
10621065
#print 'creating line collection'
1063-
coll = mcoll.LineCollection(verts, colors=colors,
1064-
linestyles=linestyles, label=label)
1065-
self.add_collection(coll, autolim=False)
1066-
coll.update(kwargs)
1066+
lines = mcoll.LineCollection(verts, colors=colors,
1067+
linestyles=linestyles, label=label)
1068+
self.add_collection(lines, autolim=False)
1069+
lines.update(kwargs)
10671070

10681071
if len(x) > 0:
1069-
minx = min(x)
1070-
maxx = max(x)
1071-
1072-
miny = min(min(ymin), min(ymax))
1073-
maxy = max(max(ymin), max(ymax))
1072+
minx = x.min()
1073+
maxx = x.max()
1074+
miny = min(ymin.min(), ymax.min())
1075+
maxy = max(ymin.max(), ymax.max())
10741076

10751077
corners = (minx, miny), (maxx, maxy)
10761078
self.update_datalim(corners)
10771079
self.autoscale_view()
10781080

1079-
return coll
1081+
return lines
10801082

10811083
@unpack_labeled_data(replace_names=["positions", "lineoffsets",
10821084
"linelengths", "linewidths",
Loading
Loading
Loading
Loading
Loading
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,6 +2803,90 @@ def test_eb_line_zorder():
28032803
ax.set_title("errorbar zorder test")
28042804

28052805

2806+
@image_comparison(
2807+
baseline_images=['vlines_basic', 'vlines_with_nan', 'vlines_masked'],
2808+
extensions=['png']
2809+
)
2810+
def test_vlines():
2811+
# normal
2812+
x1 = [2, 3, 4, 5, 7]
2813+
y1 = [2, -6, 3, 8, 2]
2814+
fig1, ax1 = plt.subplots()
2815+
ax1.vlines(x1, 0, y1, colors='g', linewidth=5)
2816+
2817+
# GH #7406
2818+
x2 = [2, 3, 4, 5, 6, 7]
2819+
y2 = [2, -6, 3, 8, np.nan, 2]
2820+
fig2, (ax2, ax3, ax4) = plt.subplots(nrows=3, figsize=(4, 8))
2821+
ax2.vlines(x2, 0, y2, colors='g', linewidth=5)
2822+
2823+
x3 = [2, 3, 4, 5, 6, 7]
2824+
y3 = [np.nan, 2, -6, 3, 8, 2]
2825+
ax3.vlines(x3, 0, y3, colors='r', linewidth=3, linestyle='--')
2826+
2827+
x4 = [2, 3, 4, 5, 6, 7]
2828+
y4 = [np.nan, 2, -6, 3, 8, np.nan]
2829+
ax4.vlines(x4, 0, y4, colors='k', linewidth=2)
2830+
2831+
# tweak the x-axis so we can see the lines better
2832+
for ax in [ax1, ax2, ax3, ax4]:
2833+
ax.set_xlim(0, 10)
2834+
2835+
# check that the y-lims are all automatically the same
2836+
assert ax1.get_ylim() == ax2.get_ylim()
2837+
assert ax1.get_ylim() == ax3.get_ylim()
2838+
assert ax1.get_ylim() == ax4.get_ylim()
2839+
2840+
fig3, ax5 = plt.subplots()
2841+
x5 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8)
2842+
ymin5 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2)
2843+
ymax5 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18)
2844+
ax5.vlines(x5, ymin5, ymax5, colors='k', linewidth=2)
2845+
ax5.set_xlim(0, 15)
2846+
2847+
2848+
@image_comparison(
2849+
baseline_images=['hlines_basic', 'hlines_with_nan', 'hlines_masked'],
2850+
extensions=['png']
2851+
)
2852+
def test_hlines():
2853+
# normal
2854+
y1 = [2, 3, 4, 5, 7]
2855+
x1 = [2, -6, 3, 8, 2]
2856+
fig1, ax1 = plt.subplots()
2857+
ax1.hlines(y1, 0, x1, colors='g', linewidth=5)
2858+
2859+
# GH #7406
2860+
y2 = [2, 3, 4, 5, 6, 7]
2861+
x2 = [2, -6, 3, 8, np.nan, 2]
2862+
fig2, (ax2, ax3, ax4) = plt.subplots(nrows=3, figsize=(4, 8))
2863+
ax2.hlines(y2, 0, x2, colors='g', linewidth=5)
2864+
2865+
y3 = [2, 3, 4, 5, 6, 7]
2866+
x3 = [np.nan, 2, -6, 3, 8, 2]
2867+
ax3.hlines(y3, 0, x3, colors='r', linewidth=3, linestyle='--')
2868+
2869+
y4 = [2, 3, 4, 5, 6, 7]
2870+
x4 = [np.nan, 2, -6, 3, 8, np.nan]
2871+
ax4.hlines(y4, 0, x4, colors='k', linewidth=2)
2872+
2873+
# tweak the y-axis so we can see the lines better
2874+
for ax in [ax1, ax2, ax3, ax4]:
2875+
ax.set_ylim(0, 10)
2876+
2877+
# check that the x-lims are all automatically the same
2878+
assert ax1.get_xlim() == ax2.get_xlim()
2879+
assert ax1.get_xlim() == ax3.get_xlim()
2880+
assert ax1.get_xlim() == ax4.get_xlim()
2881+
2882+
fig3, ax5 = plt.subplots()
2883+
y5 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8)
2884+
xmin5 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2)
2885+
xmax5 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18)
2886+
ax5.hlines(y5, xmin5, xmax5, colors='k', linewidth=2)
2887+
ax5.set_ylim(0, 15)
2888+
2889+
28062890
@image_comparison(baseline_images=['step_linestyle', 'step_linestyle'],
28072891
remove_text=True)
28082892
def test_step_linestyle():

0 commit comments

Comments
 (0)
0