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

Skip to content

Commit d7b8992

Browse files
authored
Merge pull request #7408 from phobson/vlines-with-nan
[MRG] Handle nan/masked values Axes.vlines and hlines
2 parents ba019f7 + 1b3fb65 commit d7b8992

File tree

8 files changed

+106
-19
lines changed

8 files changed

+106
-19
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,8 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
919919
self.autoscale_view(scaley=False)
920920
return p
921921

922-
@_preprocess_data(replace_names=['y', 'xmin', 'xmax'], label_namer="y")
922+
@_preprocess_data(replace_names=['y', 'xmin', 'xmax', 'colors'],
923+
label_namer='y')
923924
def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
924925
label='', **kwargs):
925926
"""
@@ -972,17 +973,18 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
972973
if not iterable(xmax):
973974
xmax = [xmax]
974975

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

978+
y = np.ravel(y)
977979
xmin = np.resize(xmin, y.shape)
978980
xmax = np.resize(xmax, y.shape)
979981

980982
verts = [((thisxmin, thisy), (thisxmax, thisy))
981983
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)
984+
lines = mcoll.LineCollection(verts, colors=colors,
985+
linestyles=linestyles, label=label)
986+
self.add_collection(lines, autolim=False)
987+
lines.update(kwargs)
986988

987989
if len(y) > 0:
988990
minx = min(xmin.min(), xmax.min())
@@ -995,10 +997,10 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
995997
self.update_datalim(corners)
996998
self.autoscale_view()
997999

998-
return coll
1000+
return lines
9991001

1000-
@_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"],
1001-
label_namer="x")
1002+
@_preprocess_data(replace_names=['x', 'ymin', 'ymax', 'colors'],
1003+
label_namer='x')
10021004
def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
10031005
label='', **kwargs):
10041006
"""
@@ -1053,30 +1055,31 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
10531055
if not iterable(ymax):
10541056
ymax = [ymax]
10551057

1058+
x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax)
1059+
10561060
x = np.ravel(x)
10571061
ymin = np.resize(ymin, x.shape)
10581062
ymax = np.resize(ymax, x.shape)
10591063

10601064
verts = [((thisx, thisymin), (thisx, thisymax))
10611065
for thisx, thisymin, thisymax in zip(x, ymin, ymax)]
10621066
#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)
1067+
lines = mcoll.LineCollection(verts, colors=colors,
1068+
linestyles=linestyles, label=label)
1069+
self.add_collection(lines, autolim=False)
1070+
lines.update(kwargs)
10671071

10681072
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))
1073+
minx = x.min()
1074+
maxx = x.max()
1075+
miny = min(ymin.min(), ymax.min())
1076+
maxy = max(ymin.max(), ymax.max())
10741077

10751078
corners = (minx, miny), (maxx, maxy)
10761079
self.update_datalim(corners)
10771080
self.autoscale_view()
10781081

1079-
return coll
1082+
return lines
10801083

10811084
@_preprocess_data(replace_names=["positions", "lineoffsets",
10821085
"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
@@ -2812,6 +2812,90 @@ def test_eb_line_zorder():
28122812
ax.set_title("errorbar zorder test")
28132813

28142814

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

0 commit comments

Comments
 (0)
0