diff --git a/examples/api/barchart_demo.py b/examples/api/barchart_demo.py index eceb5dc3aa09..19f863c5e240 100644 --- a/examples/api/barchart_demo.py +++ b/examples/api/barchart_demo.py @@ -1,41 +1,78 @@ - #!/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(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 + + 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') + + plt.show() + -fig, ax = plt.subplots() -rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd) -womenMeans = (25, 32, 34, 20, 25) -womenStd = (3, 5, 2, 3, 3) -rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd) +#---------------------- +menMeans = (20, 35, 30, 35, 27) +menStd = (2, 3, 4, 1, 2) -# 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')) +womenMeans = (25, 32, 34, 20, 25) +womenStd = (3, 5, 2, 3, 3) -ax.legend((rects1[0], rects2[0]), ('Men', 'Women')) +toddlersM = [15,10,22,25,13] +toddlersS = [3,2,4,5,1] +infantsM = [15,15,15,15,15] +infantsS = [4,5,2,3,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') +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'] + +#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() -autolabel(rects1) -autolabel(rects2) +barchart(ax, yLabel, xLabel, fig_title, bar_width, lst_bar_heights, lst_stdevs, lst_bar_colors, lst_legends=lst_legends) -plt.show() 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 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)