Permalink
Browse files

Merge branch 'errorbars' of https://github.com/chrodan/matplotlib int…

…o chrodan-errorbars

Conflicts:
	CHANGELOG
  • Loading branch information...
2 parents bf81821 + b674fc1 commit 48590f3c6ca707b47e501dff4595b733368bf064 @WeatherGod WeatherGod committed Feb 29, 2012
Showing with 69 additions and 17 deletions.
  1. +3 −0 CHANGELOG
  2. +30 −0 examples/pylab_examples/errorbar_subsample.py
  3. +34 −15 lib/matplotlib/axes.py
  4. +2 −2 lib/matplotlib/pyplot.py
View
3 CHANGELOG
@@ -1,3 +1,6 @@
+2012-02-29 errorevery keyword added to errorbar to enable errorbar
+ subsampling. fixes issue #600.
+
2012-01-08 Add axes.streamplot to plot streamlines of a velocity field.
Adapted from Tom Flannaghan streamplot implementation. -TSY
View
30 examples/pylab_examples/errorbar_subsample.py
@@ -0,0 +1,30 @@
+'''
+Demo for the errorevery keyword to show data full accuracy data plots with
+few errorbars.
+'''
+
+import numpy as np
+import matplotlib.pyplot as plt
+
+# example data
+x = np.arange(0.1, 4, 0.1)
+y = np.exp(-x)
+
+# example variable error bar values
+yerr = 0.1 + 0.1*np.sqrt(x)
+
+
+# Now switch to a more OO interface to exercise more features.
+fig, axs = plt.subplots(nrows=1, ncols=2, sharex=True)
+ax = axs[0]
+ax.errorbar(x, y, yerr=yerr)
+ax.set_title('all errorbars')
+
+ax = axs[1]
+ax.errorbar(x, y, yerr=yerr, errorevery=5)
+ax.set_title('only every 5th errorbar')
+
+
+fig.suptitle('Errorbar subsampling for better visualibility')
+
+plt.show()
View
49 lib/matplotlib/axes.py
@@ -5108,7 +5108,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
def errorbar(self, x, y, yerr=None, xerr=None,
fmt='-', ecolor=None, elinewidth=None, capsize=3,
barsabove=False, lolims=False, uplims=False,
- xlolims=False, xuplims=False, **kwargs):
+ xlolims=False, xuplims=False, errorevery=1, **kwargs):
"""
call signature::
@@ -5157,6 +5157,11 @@ def errorbar(self, x, y, yerr=None, xerr=None,
only upper/lower limits. In that case a caret symbol is
used to indicate this. lims-arguments may be of the same
type as *xerr* and *yerr*.
+
+ *errorevery*: positive integer
+ subsamples the errorbars. Eg if everyerror=5, errorbars for every
+ 5-th datapoint will be plotted. The data plot itself still shows
+ all data points.
All other keyword arguments are passed on to the plot command for the
markers. For example, this code makes big red squares with
@@ -5191,6 +5196,9 @@ def errorbar(self, x, y, yerr=None, xerr=None,
"""
+ if errorevery < 1:
+ raise ValueError('errorevery has to be a strictly positive integer ')
+
self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
if not self._hold: self.cla()
holdstate = self._hold
@@ -5247,6 +5255,8 @@ def errorbar(self, x, y, yerr=None, xerr=None,
if not iterable(xuplims): xuplims = np.array([xuplims]*len(x), bool)
else: xuplims = np.asarray(xuplims, bool)
+ everymask = np.arange(len(x)) % errorevery == 0
+
def xywhere(xs, ys, mask):
"""
return xs[mask], ys[mask] where mask is True but xs and
@@ -5285,33 +5295,38 @@ def xywhere(xs, ys, mask):
right = [thisx+thiserr for (thisx, thiserr)
in cbook.safezip(x,xerr)]
- barcols.append( self.hlines(y, left, right, **lines_kw ) )
+ yo, _ = xywhere(y, right, everymask)
+ lo, ro= xywhere(left, right, everymask)
+ barcols.append( self.hlines(yo, lo, ro, **lines_kw ) )
if capsize > 0:
if xlolims.any():
# can't use numpy logical indexing since left and
# y are lists
- leftlo, ylo = xywhere(left, y, xlolims)
+ leftlo, ylo = xywhere(left, y, xlolims & everymask)
caplines.extend(
self.plot(leftlo, ylo, ls='None',
marker=mlines.CARETLEFT, **plot_kw) )
xlolims = ~xlolims
- leftlo, ylo = xywhere(left, y, xlolims)
+ leftlo, ylo = xywhere(left, y, xlolims & everymask)
caplines.extend( self.plot(leftlo, ylo, 'k|', **plot_kw) )
else:
- caplines.extend( self.plot(left, y, 'k|', **plot_kw) )
+
+ leftlo, ylo = xywhere(left, y, everymask)
+ caplines.extend( self.plot(leftlo, ylo, 'k|', **plot_kw) )
if xuplims.any():
- rightup, yup = xywhere(right, y, xuplims)
+ rightup, yup = xywhere(right, y, xuplims & everymask)
caplines.extend(
self.plot(rightup, yup, ls='None',
marker=mlines.CARETRIGHT, **plot_kw) )
xuplims = ~xuplims
- rightup, yup = xywhere(right, y, xuplims)
+ rightup, yup = xywhere(right, y, xuplims & everymask)
caplines.extend( self.plot(rightup, yup, 'k|', **plot_kw) )
else:
- caplines.extend( self.plot(right, y, 'k|', **plot_kw) )
+ rightup, yup = xywhere(right, y, everymask)
+ caplines.extend( self.plot(rightup, yup, 'k|', **plot_kw) )
if yerr is not None:
if (iterable(yerr) and len(yerr)==2 and
@@ -5328,31 +5343,35 @@ def xywhere(xs, ys, mask):
upper = [thisy+thiserr for (thisy, thiserr)
in cbook.safezip(y,yerr)]
- barcols.append( self.vlines(x, lower, upper, **lines_kw) )
+ xo, _ = xywhere(x, lower, everymask)
+ lo, uo= xywhere(lower, upper, everymask)
+ barcols.append( self.vlines(xo, lo, uo, **lines_kw) )
if capsize > 0:
if lolims.any():
- xlo, lowerlo = xywhere(x, lower, lolims)
+ xlo, lowerlo = xywhere(x, lower, lolims & everymask)
caplines.extend(
self.plot(xlo, lowerlo, ls='None',
marker=mlines.CARETDOWN, **plot_kw) )
lolims = ~lolims
- xlo, lowerlo = xywhere(x, lower, lolims)
+ xlo, lowerlo = xywhere(x, lower, lolims & everymask)
caplines.extend( self.plot(xlo, lowerlo, 'k_', **plot_kw) )
else:
- caplines.extend( self.plot(x, lower, 'k_', **plot_kw) )
+ xlo, lowerlo = xywhere(x, lower, everymask)
+ caplines.extend( self.plot(xlo, lowerlo, 'k_', **plot_kw) )
if uplims.any():
- xup, upperup = xywhere(x, upper, uplims)
+ xup, upperup = xywhere(x, upper, uplims & everymask)
caplines.extend(
self.plot(xup, upperup, ls='None',
marker=mlines.CARETUP, **plot_kw) )
uplims = ~uplims
- xup, upperup = xywhere(x, upper, uplims)
+ xup, upperup = xywhere(x, upper, uplims & everymask)
caplines.extend( self.plot(xup, upperup, 'k_', **plot_kw) )
else:
- caplines.extend( self.plot(x, upper, 'k_', **plot_kw) )
+ xup, upperup = xywhere(x, upper, everymask)
+ caplines.extend( self.plot(xup, upperup, 'k_', **plot_kw) )
if not barsabove and fmt is not None:
l0, = self.plot(x,y,fmt,**kwargs)
View
4 lib/matplotlib/pyplot.py
@@ -2237,15 +2237,15 @@ def csd(x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.windo
# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
@autogen_docstring(Axes.errorbar)
-def errorbar(x, y, yerr=None, xerr=None, fmt='-', ecolor=None, elinewidth=None, capsize=3, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, hold=None, **kwargs):
+def errorbar(x, y, yerr=None, xerr=None, fmt='-', ecolor=None, elinewidth=None, capsize=3, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, hold=None, **kwargs):
ax = gca()
# allow callers to override the hold state by passing hold=True|False
washold = ax.ishold()
if hold is not None:
ax.hold(hold)
try:
- ret = ax.errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, **kwargs)
+ ret = ax.errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, **kwargs)
draw_if_interactive()
finally:
ax.hold(washold)

0 comments on commit 48590f3

Please sign in to comment.