Last active
January 3, 2016 17:38
-
-
Save tansey/8496589 to your computer and use it in GitHub Desktop.
Plot means with confidence (stderr or other metric) bands in a pretty format using matplotlib.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plot_with_bands(graph_title, means, bands, series, xvals=None, xlabel=None, ylabel=None, subtitle=None, filename='results.pdf'): | |
colors = ['blue','red','green', 'black', 'yellow', 'orange', 'purple', 'brown'] # max 8 lines | |
print 'means: {0} bands: {1}'.format(means.shape, bands.shape) | |
assert(means.shape == bands.shape) | |
assert(xvals is None or xvals.shape[0] == means.shape[1]) | |
assert(means.shape[1] <= len(colors)) | |
if xvals is None: | |
xvals = np.arange(means.shape[0]) | |
ax = plt.subplot(111) | |
plt.ticklabel_format(axis='y', style='plain', useOffset=False) | |
for i,mean in enumerate(means): | |
print 'plotting {0}'.format(mean) | |
plt.plot(xvals, mean, label=series[i], color=colors[i]) | |
plt.fill_between(xvals, mean + bands[i], mean - bands[i], facecolor=colors[i], alpha=0.2) | |
if xlabel is not None: | |
plt.xlabel(xlabel) | |
if ylabel is not None: | |
plt.ylabel(ylabel) | |
if subtitle is not None: | |
plt.title('{0}\n{1}'.format(graph_title, subtitle)) | |
else: | |
plt.title('{0}'.format(graph_title)) | |
# Shink current axis by 20% | |
box = ax.get_position() | |
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) | |
# Put a legend to the right of the current axis | |
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) | |
plt.savefig(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment