Added a new example to create error boxes using a PatchCollection #6596

Merged
merged 3 commits into from Jun 18, 2016

Conversation

Projects
None yet
8 participants
Contributor

tmdavison commented Jun 16, 2016

Adding a new example I first created here: http://stackoverflow.com/a/37856171/588071

@tmdavison tmdavison Added a new example to create error boxes using a PatchCollection
cd778db

mdboom added the needs_review label Jun 16, 2016

Owner

tacaswell commented Jun 16, 2016

Thanks for doing this!

Can you run this through a pep8 linter?

closes #6593

tacaswell added this to the 2.1 (next point release) milestone Jun 16, 2016

@tmdavison tmdavison Modfied for PEP8 compatibility
7f412e8
Contributor

tmdavison commented Jun 16, 2016

No problem, I think that should be ok now?

@WeatherGod WeatherGod and 1 other commented on an outdated diff Jun 16, 2016

examples/statistics/errorbars_and_boxes.py
+
+# Dummy errors (above and below)
+xerr = np.random.rand(2, n)
+yerr = np.random.rand(2, n)
+
+# Create figure and axes
+fig, ax = plt.subplots(1)
+
+# Plot data points
+ax.errorbar(x, y, xerr=xerr, yerr=yerr, fmt='None', ecolor='k')
+
+
+def makeErrorBoxes(xdata, ydata, xerror, yerror, fc='r', ec='None', alpha=0.5):
+ '''
+ Function to create error boxes
+ '''
@WeatherGod

WeatherGod Jun 16, 2016

Member

I forget, did we want examples to have functions like this when it is just being called once? I thought it was only desirable to do for the examples to de-duplicate code?

@QuLogic

QuLogic Jun 16, 2016

Member

I agree, in such a short example, there's not much benefit to this function, and there are more than enough comments to make it clear what all the code is doing.

@WeatherGod WeatherGod and 1 other commented on an outdated diff Jun 16, 2016

examples/statistics/errorbars_and_boxes.py
+ # Loop over data points; create box from errors at each point
+ for xc, yc, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
+ rect = Rectangle((xc-xe[0], yc-ye[0]), xe.sum(), ye.sum())
+ errorboxes.append(rect)
+
+ # Create patch collection with specified colour/alpha
+ pc = PatchCollection(errorboxes, facecolor=fc, alpha=alpha, edgecolor=ec)
+
+ # Add collection to axes
+ ax.add_collection(pc)
+
+# Call function to create error boxes
+makeErrorBoxes(x, y, xerr, yerr)
+
+# Add some space around the data points on the axes
+ax.margins(0.1)
@WeatherGod

WeatherGod Jun 16, 2016

Member

do we need to do this? the less code, the better, I would think.

@tmdavison

tmdavison Jun 17, 2016

Contributor

agreed, I've removed it now

@WeatherGod WeatherGod and 3 others commented on an outdated diff Jun 16, 2016

examples/statistics/errorbars_and_boxes.py
+ # Create list for all the error patches
+ errorboxes = []
+
+ # Loop over data points; create box from errors at each point
+ for xc, yc, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
+ rect = Rectangle((xc-xe[0], yc-ye[0]), xe.sum(), ye.sum())
+ errorboxes.append(rect)
+
+ # Create patch collection with specified colour/alpha
+ pc = PatchCollection(errorboxes, facecolor=fc, alpha=alpha, edgecolor=ec)
+
+ # Add collection to axes
+ ax.add_collection(pc)
+
+# Call function to create error boxes
+makeErrorBoxes(x, y, xerr, yerr)
@WeatherGod

WeatherGod Jun 16, 2016

Member

should we be making this boxes before calling errorbar() so that the boxes appear below the errorbars?

@QuLogic

QuLogic Jun 16, 2016

Member

That depends on the zorder, not when it's put on the axes, no?

@tacaswell

tacaswell Jun 16, 2016

Owner

I think examples should always be functions so that users can copy-paste
use them.

On Thu, Jun 16, 2016, 15:22 Benjamin Root notifications@github.com wrote:

In examples/statistics/errorbars_and_boxes.py
#6596 (comment):

  • Create list for all the error patches

  • errorboxes = []
  • Loop over data points; create box from errors at each point

  • for xc, yc, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
  •    rect = Rectangle((xc-xe[0], yc-ye[0]), xe.sum(), ye.sum())
    
  •    errorboxes.append(rect)
    
  • Create patch collection with specified colour/alpha

  • pc = PatchCollection(errorboxes, facecolor=fc, alpha=alpha, edgecolor=ec)
  • Add collection to axes

  • ax.add_collection(pc)

+# Call function to create error boxes
+makeErrorBoxes(x, y, xerr, yerr)

should we be making this boxes before calling errorbar() so that the
boxes appear below the errorbars?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/matplotlib/matplotlib/pull/6596/files/7f412e8d59060ce61af0f111d70bc42c5c15985e#r67408151,
or mute the thread
https://github.com/notifications/unsubscribe/AAMMhXKKiv5idEDU6eJ_NxIHyM93Jpj1ks5qMaKJgaJpZM4I3grL
.

@efiring

efiring Jun 16, 2016

Owner

Maybe not always, but in cases where they encapsulate reusable functionality, yes. In the present case, the function needs to include ax as an initial argument.

Contributor

anntzer commented Jun 17, 2016

Some suggestions (partially rendundant with other comments):

  • Rename function to plot_error_boxes; pass ax as first argument, and include the call to errorbar within the function.
  • Either write a complete docstring, or just drop it. (Documenting makeErrorBoxes as "Function to create error boxes" is... not very helpful.)
@tmdavison tmdavison Some style changes to address comments on the pull request
62f58e2

@WeatherGod WeatherGod merged commit 25ce666 into matplotlib:master Jun 18, 2016

1 of 3 checks passed

continuous-integration/appveyor/pr AppVeyor build failed
Details
continuous-integration/travis-ci/pr The Travis CI build failed
Details
coverage/coveralls Coverage remained the same at 70.242%
Details

mdboom removed the needs_review label Jun 18, 2016

Member

WeatherGod commented Jun 18, 2016

does this get backported anywhere?

Owner

jenshnielsen commented Jun 18, 2016

I think it should go into 2.x too

@WeatherGod WeatherGod added a commit that referenced this pull request Jul 14, 2016

@WeatherGod WeatherGod Merge pull request #6596 from tmdavison/master
Added a new example to create error boxes using a PatchCollection
9ea7315
Member

WeatherGod commented Jul 14, 2016

backported to v2.x via 9ea7315

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment