Browse files

Merge pull request #1149 from dmcdougall/percent

Add Phil Elson's percentage histogram example
  • Loading branch information...
2 parents 1d3faee + 1985162 commit 47a61d70d0d3e405a35b5d61dcdda84fca857826 @mdboom mdboom committed Aug 28, 2012
Showing with 29 additions and 0 deletions.
  1. +29 −0 examples/pylab_examples/histogram_percent_demo.py
View
29 examples/pylab_examples/histogram_percent_demo.py
@@ -0,0 +1,29 @@
+import matplotlib
+from numpy.random import randn
+import matplotlib.pyplot as plt
+from matplotlib.ticker import FuncFormatter
+
+def to_percent(y, position):
+ # Ignore the passed in position. This has the effect of scaling the default
+ # tick locations.
+ s = str(100 * y)
+
+ # The percent symbol needs escaping in latex
+ if matplotlib.rcParams['text.usetex'] == True:
+ return s + r'$\%$'
+ else:
+ return s + '%'
+
+x = randn(5000)
+
+# Make a normed histogram. It'll be multiplied by 100 later.
+plt.hist(x, bins=50, normed=True)
+
+# Create the formatter using the function to_percent. This multiplies all the
+# default labels by 100, making them all percentages
+formatter = FuncFormatter(to_percent)
+
+# Set the formatter
+plt.gca().yaxis.set_major_formatter(formatter)
+
+plt.show()

0 comments on commit 47a61d7

Please sign in to comment.