From ca7e482cf81fa0ab589c4f4201f6a4000a78682f Mon Sep 17 00:00:00 2001 From: eduardoh28 Date: Sat, 27 Sep 2014 13:43:30 -0400 Subject: [PATCH 1/4] created function wrapper for barchart example --- examples/api/barchart_demo.py | 90 ++++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/examples/api/barchart_demo.py b/examples/api/barchart_demo.py index eceb5dc3aa09..d41839893500 100644 --- a/examples/api/barchart_demo.py +++ b/examples/api/barchart_demo.py @@ -1,41 +1,77 @@ - #!/usr/bin/env python # a bar plot with errorbars import numpy as np import matplotlib.pyplot as plt +import sys -N = 5 -menMeans = (20, 35, 30, 35, 27) -menStd = (2, 3, 4, 1, 2) +#---------------------- +def barchart(yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_bar_colors, lst_legends=[]): + + #enforce that for every set of bars there's a corresponding set of stdevs, and a bar color + if ( (len(lst_bar_heights)!=len(lst_stdevs)) or (len(lst_bar_heights)!=len(lst_bar_colors)) or (len(lst_bar_heights)!=len(lst_bar_colors)) ): + print "lst_bar_heights (len=%d), lst_stdevs (len=%d), lst_bar_colors (len=%d) must have the same number of elements" %(len(lst_bar_heights), len(lst_stdevs), len(lst_bar_colors)) + sys.exit() + + if lst_legends: + if not (len(lst_bar_heights)==len(lst_legends)): + print "lst_bar_heights and lst_legends must have the same number of elements" + sys.exit() + + #enforce that for each bar height there's a corresponding stdev + for bar_height_set, stdev_set in zip(lst_bar_heights, lst_stdevs): + if len(bar_height_set)!=len(stdev_set): + print "lst_bar_height and lst_stdevs don't have the same length" + print "bar height values (len=%d): %s" %(len(bar_height_set), bar_height_set) + print "stdev values (len=%d): %s" %(len(stdev_set), stdev_set) + sys.exit() + + nSeries = len(lst_bar_heights) + nBins = len(lst_bar_heights[0]) + ind = np.arange(nBins) # the x locations for the groups + width = bar_width # the width of the bars + fig, ax = plt.subplots() + + lst_rects = [ax.bar(ind+n*width, lst_bar_heights[n], width, color=lst_bar_colors[n], yerr=lst_stdevs[n]) for n in range(nSeries)] + ax.set_xticklabels(xLabel) + + if lst_legends: + ax.legend( lst_rects, [legend_i for legend_i in lst_legends] ) + + # add some text for labels, title and axes ticks + ax.set_ylabel(yLabel) + ax.set_title(fig_title) + ax.set_xticks(ind+width) -ind = np.arange(N) # the x locations for the groups -width = 0.35 # the width of the bars + for rect in lst_rects: + for rect_i in rect: + height = rect_i.get_height() + ax.text(rect_i.get_x()+rect_i.get_width()/2., 1.05*height, '%d'%int(height), ha='center', va='bottom') -fig, ax = plt.subplots() -rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd) + plt.show() + -womenMeans = (25, 32, 34, 20, 25) -womenStd = (3, 5, 2, 3, 3) -rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd) -# add some text for labels, title and axes ticks -ax.set_ylabel('Scores') -ax.set_title('Scores by group and gender') -ax.set_xticks(ind + width) -ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) +#---------------------- +menMeans = (20, 35, 30, 35, 27) +menStd = (2, 3, 4, 1, 2) -ax.legend((rects1[0], rects2[0]), ('Men', 'Women')) +womenMeans = (25, 32, 34, 20, 25) +womenStd = (3, 5, 2, 3, 3) +toddlersM = [15,10,22,25,13] +toddlersS = [3,2,4,5,1] -def autolabel(rects): - # attach some text labels - for rect in rects: - height = rect.get_height() - ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, - '%d' % int(height), - ha='center', va='bottom') +infantsM = [15,15,15,15,15] +infantsS = [4,5,2,3,1] -autolabel(rects1) -autolabel(rects2) +lst_bar_heights = [menMeans, womenMeans, toddlersM, infantsM] +lst_stdevs = [menStd, womenStd, toddlersS, infantsS] +yLabel = 'Scores' +xLabel = ['G1', 'G2', 'G3', 'G4', 'G5'] +fig_title = 'Scores by group and gender' +bar_width = 0.15 +lst_legends = ['Men', 'Women', 'Toddlers', 'Infants'] +lst_bar_colors = ['r','y','g','b'] + +barchart(yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_bar_colors, lst_legends=lst_legends) -plt.show() From 09a9e14c41af73366c51bfff47ad34a019183402 Mon Sep 17 00:00:00 2001 From: eduardoh28 Date: Sat, 27 Sep 2014 15:14:22 -0400 Subject: [PATCH 2/4] created wrapper around watermark_image.py --- examples/api/watermark_image.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/examples/api/watermark_image.py b/examples/api/watermark_image.py index 9e9421b7ab3d..8c37cbf1114d 100644 --- a/examples/api/watermark_image.py +++ b/examples/api/watermark_image.py @@ -1,21 +1,27 @@ """ Use a PNG file as a watermark """ + from __future__ import print_function import numpy as np import matplotlib.cbook as cbook import matplotlib.image as image import matplotlib.pyplot as plt -datafile = cbook.get_sample_data('logo2.png', asfileobj=False) -print ('loading %s' % datafile) -im = image.imread(datafile) -im[:, :, -1] = 0.5 # set the alpha channel - -fig, ax = plt.subplots() -ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange') -ax.grid() -fig.figimage(im, 10, 10) +def watermark_image(datafile): + im = image.imread(datafile) + im[:, :, -1] = 0.5 # set the alpha channel -plt.show() + fig, ax = plt.subplots() + + ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange') + ax.grid() + fig.figimage(im, 10, 10) + + plt.show() + +#------------------ +datafile = cbook.get_sample_data('logo2.png', asfileobj=False) +print ('loading %s' % datafile) +watermark_image(datafile) From 0165e57e641508421cb97c1dacc70804656caada Mon Sep 17 00:00:00 2001 From: eduardoh28 Date: Sat, 27 Sep 2014 15:24:57 -0400 Subject: [PATCH 3/4] moved data checks outside function and passed axes as first parameter --- examples/api/barchart_demo.py | 45 ++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/examples/api/barchart_demo.py b/examples/api/barchart_demo.py index d41839893500..19f863c5e240 100644 --- a/examples/api/barchart_demo.py +++ b/examples/api/barchart_demo.py @@ -5,31 +5,12 @@ import sys #---------------------- -def barchart(yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_bar_colors, lst_legends=[]): - - #enforce that for every set of bars there's a corresponding set of stdevs, and a bar color - if ( (len(lst_bar_heights)!=len(lst_stdevs)) or (len(lst_bar_heights)!=len(lst_bar_colors)) or (len(lst_bar_heights)!=len(lst_bar_colors)) ): - print "lst_bar_heights (len=%d), lst_stdevs (len=%d), lst_bar_colors (len=%d) must have the same number of elements" %(len(lst_bar_heights), len(lst_stdevs), len(lst_bar_colors)) - sys.exit() - - if lst_legends: - if not (len(lst_bar_heights)==len(lst_legends)): - print "lst_bar_heights and lst_legends must have the same number of elements" - sys.exit() - - #enforce that for each bar height there's a corresponding stdev - for bar_height_set, stdev_set in zip(lst_bar_heights, lst_stdevs): - if len(bar_height_set)!=len(stdev_set): - print "lst_bar_height and lst_stdevs don't have the same length" - print "bar height values (len=%d): %s" %(len(bar_height_set), bar_height_set) - print "stdev values (len=%d): %s" %(len(stdev_set), stdev_set) - sys.exit() - +def barchart(ax, yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_bar_colors, lst_legends=[]): + nSeries = len(lst_bar_heights) nBins = len(lst_bar_heights[0]) ind = np.arange(nBins) # the x locations for the groups width = bar_width # the width of the bars - fig, ax = plt.subplots() lst_rects = [ax.bar(ind+n*width, lst_bar_heights[n], width, color=lst_bar_colors[n], yerr=lst_stdevs[n]) for n in range(nSeries)] ax.set_xticklabels(xLabel) @@ -73,5 +54,25 @@ def barchart(yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_legends = ['Men', 'Women', 'Toddlers', 'Infants'] lst_bar_colors = ['r','y','g','b'] -barchart(yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_bar_colors, lst_legends=lst_legends) +#enforce that for every set of bars there's a corresponding set of stdevs, and a bar color +if ( (len(lst_bar_heights)!=len(lst_stdevs)) or (len(lst_bar_heights)!=len(lst_bar_colors)) or (len(lst_bar_heights)!=len(lst_bar_colors)) ): + print "lst_bar_heights (len=%d), lst_stdevs (len=%d), lst_bar_colors (len=%d) must have the same number of elements" %(len(lst_bar_heights), len(lst_stdevs), len(lst_bar_colors)) + sys.exit() + +if lst_legends: + if not (len(lst_bar_heights)==len(lst_legends)): + print "lst_bar_heights and lst_legends must have the same number of elements" + sys.exit() + +#enforce that for each bar height there's a corresponding stdev +for bar_height_set, stdev_set in zip(lst_bar_heights, lst_stdevs): + if len(bar_height_set)!=len(stdev_set): + print "lst_bar_height and lst_stdevs don't have the same length" + print "bar height values (len=%d): %s" %(len(bar_height_set), bar_height_set) + print "stdev values (len=%d): %s" %(len(stdev_set), stdev_set) + sys.exit() + +fig, ax = plt.subplots() + +barchart(ax, yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_bar_colors, lst_legends=lst_legends) From e0e2526fe311f51242fd69f2e9f42730ae81234c Mon Sep 17 00:00:00 2001 From: eduardoh28 Date: Sat, 27 Sep 2014 16:37:32 -0400 Subject: [PATCH 4/4] created wrapper around span_regions.py --- examples/api/span_regions.py | 42 +++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/api/span_regions.py b/examples/api/span_regions.py index bf23ffb07379..c280fd767bec 100644 --- a/examples/api/span_regions.py +++ b/examples/api/span_regions.py @@ -9,23 +9,31 @@ import matplotlib.collections as collections +def span_regions(ax, fig_title, x, y, plot_line_color='k', plot_line_weight=0.5, + zero_line_color='k', zero_line_weight=0.5, + pos_rgn_color='green', pos_rgn_alpha=0.5, + neg_rgn_color='red', neg_rgn_alpha=0.5, + threshold=0): + + ax.set_title(fig_title) + ax.plot(x, y, color=plot_line_color, lw=plot_line_weight) + ax.axhline(0, color=zero_line_color, lw=zero_line_weight) + + collection = collections.BrokenBarHCollection.span_where( + t, ymin=0, ymax=1, where=s > threshold, facecolor=pos_rgn_color, alpha=pos_rgn_alpha) + ax.add_collection(collection) + + collection = collections.BrokenBarHCollection.span_where( + t, ymin=-1, ymax=0, where=s < threshold, facecolor=neg_rgn_color, alpha=neg_rgn_alpha) + ax.add_collection(collection) + + plt.show() + +#-------------------------------------------------------------------------------------------------------- t = np.arange(0.0, 2, 0.01) -s1 = np.sin(2*np.pi*t) -s2 = 1.2*np.sin(4*np.pi*t) - - +s = np.sin(2*np.pi*t) fig, ax = plt.subplots() -ax.set_title('using span_where') -ax.plot(t, s1, color='black') -ax.axhline(0, color='black', lw=2) - -collection = collections.BrokenBarHCollection.span_where( - t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.5) -ax.add_collection(collection) - -collection = collections.BrokenBarHCollection.span_where( - t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5) -ax.add_collection(collection) - -plt.show() +span_regions(ax, "using span_where", t, s, plot_line_color='k', plot_line_weight=0.5, zero_line_color='k', + zero_line_weight=0.75, pos_rgn_color='green', neg_rgn_color='red', pos_rgn_alpha=0.75, + neg_rgn_alpha=0.2, threshold=0) \ No newline at end of file