Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1149 from dmcdougall/percent

Add Phil Elson's percentage histogram example
  • Loading branch information...
commit 47a61d70d0d3e405a35b5d61dcdda84fca857826 2 parents 1d3faee + 1985162
Michael Droettboom mdboom authored
Showing with 29 additions and 0 deletions.
  1. +29 −0 examples/pylab_examples/histogram_percent_demo.py
29 examples/pylab_examples/histogram_percent_demo.py
View
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.