Skip to content

created function wrapper for barchart example #3579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 64 additions & 27 deletions examples/api/barchart_demo.py
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 25 additions & 17 deletions examples/api/span_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 16 additions & 10 deletions examples/api/watermark_image.py
Original file line number Diff line number Diff line change
@@ -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)