Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 747c3cd

Browse filesBrowse files
committed
Use np.nanmin/max to set ax limits in Axes.vlines
Closes GH #7406. Also applied fixes to Axes.hlines and wrote tests for both.
1 parent bdf2146 commit 747c3cd
Copy full SHA for 747c3cd

File tree

Expand file treeCollapse file tree

8 files changed

+104
-18
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+104
-18
lines changed

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+20-18Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,8 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
965965
xmin = self.convert_xunits(xmin)
966966
xmax = self.convert_xunits(xmax)
967967

968+
y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax)
969+
968970
if not iterable(y):
969971
y = [y]
970972
if not iterable(xmin):
@@ -973,20 +975,19 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
973975
xmax = [xmax]
974976

975977
y = np.ravel(y)
976-
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:
988-
minx = min(xmin.min(), xmax.min())
989-
maxx = max(xmin.max(), xmax.max())
989+
minx = np.min([xmin.min(), xmax.min()])
990+
maxx = np.max([xmin.max(), xmax.max()])
990991
miny = y.min()
991992
maxy = y.max()
992993

@@ -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
@_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"],
10011002
label_namer="x")
@@ -1046,6 +1047,8 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
10461047
ymin = self.convert_yunits(ymin)
10471048
ymax = self.convert_yunits(ymax)
10481049

1050+
x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax)
1051+
10491052
if not iterable(x):
10501053
x = [x]
10511054
if not iterable(ymin):
@@ -1060,23 +1063,22 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
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 = np.min([ymin.min(), ymax.min()])
1075+
maxy = np.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
@_preprocess_data(replace_names=["positions", "lineoffsets",
10821084
"linelengths", "linewidths",
Loading
Loading
Loading
Loading
Loading
Loading

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+84Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.