From cd778db53bf0bbe004a601bc44b58fad2bff3052 Mon Sep 17 00:00:00 2001 From: tmdavison Date: Thu, 16 Jun 2016 16:24:01 +0100 Subject: [PATCH 1/3] Added a new example to create error boxes using a PatchCollection --- examples/statistics/errorbars_and_boxes.py | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/statistics/errorbars_and_boxes.py diff --git a/examples/statistics/errorbars_and_boxes.py b/examples/statistics/errorbars_and_boxes.py new file mode 100644 index 00000000000..b159b81dc2b --- /dev/null +++ b/examples/statistics/errorbars_and_boxes.py @@ -0,0 +1,48 @@ +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.collections import PatchCollection +from matplotlib.patches import Rectangle + +# Number of data points +n=5 + +# Dummy data +x=np.arange(0,n,1) +y=np.random.rand(n)*5. + +# Dummy errors (above and below) +xerr=np.random.rand(2,n) +yerr=np.random.rand(2,n) + +print xerr[:,0] + +# Create figure and axes +fig,ax = plt.subplots(1) + +# Plot data points +ax.errorbar(x,y,xerr=xerr,yerr=yerr,fmt='None',ecolor='k') + +# Function to plot error boxes +def makeErrorBoxes(xdata,ydata,xerror,yerror,fc='r',ec='None',alpha=0.5): + + # 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) + +# Add some space around the data points on the axes +ax.margins(0.1) + +plt.show() From 7f412e8d59060ce61af0f111d70bc42c5c15985e Mon Sep 17 00:00:00 2001 From: tmdavison Date: Thu, 16 Jun 2016 16:59:08 +0100 Subject: [PATCH 2/3] Modfied for PEP8 compatibility --- examples/statistics/errorbars_and_boxes.py | 35 +++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/examples/statistics/errorbars_and_boxes.py b/examples/statistics/errorbars_and_boxes.py index b159b81dc2b..c1d3fa917da 100644 --- a/examples/statistics/errorbars_and_boxes.py +++ b/examples/statistics/errorbars_and_boxes.py @@ -1,46 +1,51 @@ +''' +Example to create boxes from error using PatchCollection +''' + import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection from matplotlib.patches import Rectangle # Number of data points -n=5 +n = 5 # Dummy data -x=np.arange(0,n,1) -y=np.random.rand(n)*5. +x = np.arange(0, n, 1) +y = np.random.rand(n)*5. # Dummy errors (above and below) -xerr=np.random.rand(2,n) -yerr=np.random.rand(2,n) - -print xerr[:,0] +xerr = np.random.rand(2, n) +yerr = np.random.rand(2, n) # Create figure and axes -fig,ax = plt.subplots(1) +fig, ax = plt.subplots(1) # Plot data points -ax.errorbar(x,y,xerr=xerr,yerr=yerr,fmt='None',ecolor='k') +ax.errorbar(x, y, xerr=xerr, yerr=yerr, fmt='None', ecolor='k') + -# Function to plot error boxes -def makeErrorBoxes(xdata,ydata,xerror,yerror,fc='r',ec='None',alpha=0.5): +def makeErrorBoxes(xdata, ydata, xerror, yerror, fc='r', ec='None', alpha=0.5): + ''' + Function to create error boxes + ''' # 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()) + 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) + 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) +makeErrorBoxes(x, y, xerr, yerr) # Add some space around the data points on the axes ax.margins(0.1) From 62f58e2a78c19e19ca49e0f5d438fc528700686f Mon Sep 17 00:00:00 2001 From: tmdavison Date: Fri, 17 Jun 2016 09:42:06 +0100 Subject: [PATCH 3/3] Some style changes to address comments on the pull request --- examples/statistics/errorbars_and_boxes.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/examples/statistics/errorbars_and_boxes.py b/examples/statistics/errorbars_and_boxes.py index c1d3fa917da..a0a421640c5 100644 --- a/examples/statistics/errorbars_and_boxes.py +++ b/examples/statistics/errorbars_and_boxes.py @@ -21,14 +21,8 @@ # 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 - ''' +def make_error_boxes(ax, xdata, ydata, xerror, yerror, fc='r', ec='None', alpha=0.5): # Create list for all the error patches errorboxes = [] @@ -44,10 +38,11 @@ def makeErrorBoxes(xdata, ydata, xerror, yerror, fc='r', ec='None', alpha=0.5): # Add collection to axes ax.add_collection(pc) -# Call function to create error boxes -makeErrorBoxes(x, y, xerr, yerr) + # Plot errorbars + ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror, fmt='None', ecolor='k') -# Add some space around the data points on the axes -ax.margins(0.1) + +# Call function to create error boxes +make_error_boxes(ax, x, y, xerr, yerr) plt.show()