Skip to content

Instantly share code, notes, and snippets.

@phobson
Created May 27, 2012 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phobson/2814818 to your computer and use it in GitHub Desktop.
Save phobson/2814818 to your computer and use it in GitHub Desktop.
Script to test my exits to axes.boxplot()
'''
This demonstrates the new boxplot functionality.
It trys to create 5 figures, each with 4 subplots. Only the first
fourth figures should work. The second and third figures are
supposed to fail as the number of user-input medians and
confidence intervals do not match the number of columns
in the data.
The first working figure plots all of the data with and without
user-input medians and confidence intervals just as you would
normally expect from axes.boxplot
The fourth figure plots only 1 column of data and demonstrates how
the user-input median and confidence interval still need to
be stuff inside of a list. I'm not happy with the this, but
it is consistent with how the `position` kwargs behaves.
The fifth figure is the same as the first, only with numpy arrays
as input instead of lists.
'''
import pdb
import numpy as np
import matplotlib.pyplot as plt
data = np.random.normal(loc=3.50, scale=1.25, size=(150,4))
myConfIntervals = [None, (1., 5.), None, (3.0, 5)]
myConfIntervalArray = np.array([(2., 4.), (1., 5.), (2.5, 4.5), (3.0, 5)])
myMedians = [None, None, 3.6, 3.7]
npMedians = np.median(data, axis=0)
# case that should work
try:
fig0, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].boxplot(data, bootstrap=None, notch=1)
axes[0,1].boxplot(data, bootstrap=None, notch=1,
usermedians=myMedians,
conf_intervals=myConfIntervals)
axes[1,0].boxplot(data, bootstrap=10000, notch=1)
axes[1,1].boxplot(data, bootstrap=10000, notch=1,
usermedians=myMedians,
conf_intervals=myConfIntervals)
axes[0,0].set_title("No bootstraps, all MPL")
axes[0,1].set_title("No bootstraps, user-spec'd")
axes[1,0].set_title("Bootstraps, all MPL")
axes[1,1].set_title("Bootstraps, user-spec'd")
fig0.suptitle('Several columns of data, list/list of tuples input')
except:
plt.close(fig0)
print('that should have worked')
# should fail with too many user-spec'd params
try:
fig1, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].boxplot(data[:,1:], bootstrap=None, notch=1)
axes[0,1].boxplot(data[:,1:], bootstrap=None, notch=1,
usermedians=myMedians,
conf_intervals=myConfIntervals)
axes[1,0].boxplot(data[:,1:], bootstrap=10000, notch=1)
axes[1,1].boxplot(data[:,1:], bootstrap=10000, notch=1,
usermedians=myMedians,
conf_intervals=myConfIntervals)
axes[0,0].set_title("No bootstraps, all MPL")
axes[0,1].set_title("No bootstraps, user-spec'd")
axes[1,0].set_title("Bootstraps, all MPL")
axes[1,1].set_title("Bootstraps, user-spec'd")
except:
plt.close(fig1)
print('supposed to fail, too many user-specd medians and CIs')
# should fail with too few user-spec'd params
try:
fig2, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].boxplot(data, bootstrap=None, notch=1)
axes[0,1].boxplot(data, bootstrap=None, notch=1,
usermedians=myMedians[1:],
conf_intervals=myConfIntervals[1:])
axes[1,0].boxplot(data, bootstrap=10000, notch=1)
axes[1,1].boxplot(data, bootstrap=10000, notch=1,
usermedians=myMedians[1:],
conf_intervals=myConfIntervals[1:])
axes[0,0].set_title("No bootstraps, all MPL")
axes[0,1].set_title("No bootstraps, user-spec'd")
axes[1,0].set_title("Bootstraps, all MPL")
axes[1,1].set_title("Bootstraps, user-spec'd")
except:
plt.close(fig2)
print('supposed to fail, too few user-specd medians and CIs')
# plotting only one column of data -- still need user-spec'd
# params to be insides lists
try:
fig3, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].boxplot(data[:,0], bootstrap=None, notch=1)
axes[0,1].boxplot(data[:,0], bootstrap=None, notch=1,
usermedians=[4.0],
conf_intervals=[(3.,4.3)])
axes[1,0].boxplot(data[:,0], bootstrap=10000, notch=1)
axes[1,1].boxplot(data[:,0], bootstrap=10000, notch=1,
usermedians=[4.0],
conf_intervals=[(3.,4.3)])
axes[0,0].set_title("No bootstraps, all MPL")
axes[0,1].set_title("No bootstraps, user-spec'd")
axes[1,0].set_title("Bootstraps, all MPL")
axes[1,1].set_title("Bootstraps, user-spec'd")
fig3.suptitle('One column of data, list/list of tuples input')
except:
plt.close(fig3)
print('should have worked')
# plotting many columns of data -- user-spec'd params as np.arrays
#
try:
fig4, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].boxplot(data, bootstrap=None, notch=1)
axes[0,1].boxplot(data, bootstrap=None, notch=1,
usermedians=npMedians,
conf_intervals=myConfIntervalArray)
axes[1,0].boxplot(data, bootstrap=10000, notch=1)
axes[1,1].boxplot(data, bootstrap=10000, notch=1,
usermedians=npMedians,
conf_intervals=myConfIntervalArray)
axes[0,0].set_title("No bootstraps, all MPL")
axes[0,1].set_title("No bootstraps, user-spec'd")
axes[1,0].set_title("Bootstraps, all MPL")
axes[1,1].set_title("Bootstraps, user-spec'd")
fig4.suptitle('Several columns of data, numpy arrays as input')
except Exception as msg:
plt.close(fig4)
print('should have worked')
print(msg)
pdb.set_trace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment