8000 Handle nan/masked values Axes.vlines and hlines by phobson · Pull Request #7408 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Handle nan/masked values Axes.vlines and hlines #7408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,8 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
self.autoscale_view(scaley=False)
return p

@_preprocess_data(replace_names=['y', 'xmin', 'xmax'], label_namer="y")
@_preprocess_data(replace_names=['y', 'xmin', 'xmax', 'colors'],
label_namer='y')
def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
label='', **kwargs):
"""
Expand Down Expand Up @@ -972,17 +973,18 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
if not iterable(xmax):
xmax = [xmax]

y = np.ravel(y)
y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax)

y = np.ravel(y)
xmin = np.resize(xmin, y.shape)
xmax = np.resize(xmax, y.shape)

verts = [((thisxmin, thisy), (thisxmax, thisy))
for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)]
coll = mcoll.LineCollection(verts, colors=colors,
linestyles=linestyles, label=label)
self.add_collection(coll, autolim=False)
coll.update(kwargs)
lines = mcoll.LineCollection(verts, colors=colors,
linestyles=linestyles, label=label)
self.add_collection(lines, autolim=False)
lines.update(kwargs)

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

return coll
return lines

@_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"],
label_namer="x")
@_preprocess_data(replace_names=['x', 'ymin', 'ymax', 'colors'],
label_namer='x')
def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
label='', **kwargs):
"""
Expand Down Expand Up @@ -1053,30 +1055,31 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
if not iterable(ymax):
ymax = [ymax]

x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax)

x = np.ravel(x)
ymin = np.resize(ymin, x.shape)
ymax = np.resize(ymax, x.shape)

verts = [((thisx, thisymin), (thisx, thisymax))
for thisx, thisymin, thisymax in zip(x, ymin, ymax)]
#print 'creating line collection'
coll = mcoll.LineCollection(verts, colors=colors,
linestyles=linestyles, label=label)
self.add_collection(coll, autolim=False)
coll.update(kwargs)
lines = mcoll.LineCollection(verts, colors=colors,
linestyles=linestyles, label=label)
self.add_collection(lines, autolim=False)
lines.update(kwargs)

if len(x) > 0:
minx = min(x)
maxx = max(x)

miny = min(min(ymin), min(ymax))
maxy = max(max(ymin), max(ymax))
minx = x.min()
maxx = x.max()
miny = min(ymin.min(), ymax.min())
maxy = max(ymin.max(), ymax.max())

corners = (minx, miny), (maxx, maxy)
self.update_datalim(corners)
self.autoscale_view()

return coll
return lines

@_preprocess_data(replace_names=["positions", "lineoffsets",
"linelengths", "linewidths",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2812,6 +2812,90 @@ def test_eb_line_zorder():
ax.set_title("errorbar zorder test")


@image_comparison(
baseline_images=['vlines_basic', 'vlines_with_nan', 'vlines_masked'],
extensions=['png']
)
def test_vlines():
# normal
x1 = [2, 3, 4, 5, 7]
y1 = [2, -6, 3, 8, 2]
fig1, ax1 = plt.subplots()
ax1.vlines(x1, 0, y1, colors='g', linewidth=5)

# GH #7406
x2 = [2, 3, 4, 5, 6, 7]
y2 = [2, -6, 3, 8, np.nan, 2]
fig2, (ax2, ax3, ax4) = plt.subplots(nrows=3, figsize=(4, 8))
ax2.vlines(x2, 0, y2, colors='g', linewidth=5)

x3 = [2, 3, 4, 5, 6, 7]
y3 = [np.nan, 2, -6, 3, 8, 2]
ax3.vlines(x3, 0, y3, colors='r', linewidth=3, linestyle='--')

x4 = [2, 3, 4, 5, 6, 7]
y4 = [np.nan, 2, -6, 3, 8, np.nan]
ax4.vlines(x4, 0, y4, colors='k', linewidth=2)

# tweak the x-axis so we can see the lines better
for ax in [ax1, ax2, ax3, ax4]:
ax.set_xlim(0, 10)

# check that the y-lims are all automatically the same
assert ax1.get_ylim() == ax2.get_ylim()
assert ax1.get_ylim() == ax3.get_ylim()
assert ax1.get_ylim() == ax4.get_ylim()

fig3, ax5 = plt.subplots()
x5 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8)
ymin5 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2)
ymax5 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18)
ax5.vlines(x5, ymin5, ymax5, colors='k', linewidth=2)
ax5.set_xlim(0, 15)


@image_comparison(
baseline_images=['hlines_basic', 'hlines_with_nan', 'hlines_masked'],
extensions=['png']
)
def test_hlines():
# normal
y1 = [2, 3, 4, 5, 7]
x1 = [2, -6, 3, 8, 2]
fig1, ax1 = plt.subplots()
ax1.hlines(y1, 0, x1, colors='g', linewidth=5)

# GH #7406
y2 = [2, 3, 4, 5, 6, 7]
x2 = [2, -6, 3, 8, np.nan, 2]
fig2, (ax2, ax3, ax4) = plt.subplots(nrows=3, figsize=(4, 8))
ax2.hlines(y2, 0, x2, colors='g', linewidth=5)

y3 = [2, 3, 4, 5, 6, 7]
x3 = [np.nan, 2, -6, 3, 8, 2]
ax3.hlines(y3, 0, x3, colors='r', linewidth=3, linestyle='--')

y4 = [2, 3, 4, 5, 6, 7]
x4 = [np.nan, 2, -6, 3, 8, np.nan]
ax4.hlines(y4, 0, x4, colors='k', linewidth=2)

# tweak the y-axis so we can see the lines better
for ax in [ax1, ax2, ax3, ax4]:
ax.set_ylim(0, 10)

# check that the x-lims are all automatically the same
assert ax1.get_xlim() == ax2.get_xlim()
assert ax1.get_xlim() == ax3.get_xlim()
assert ax1.get_xlim() == ax4.get_xlim()

fig3, ax5 = plt.subplots()
y5 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8)
xmin5 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2)
xmax5 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18)
ax5.hlines(y5, xmin5, xmax5, colors='k', linewidth=2)
ax5.set_ylim(0, 15)


@image_comparison(baseline_images=['step_linestyle', 'step_linestyle'],
remove_text=True)
def test_step_linestyle():
Expand Down
0