Skip to content

Commit 0c22d3b

Browse files
committed
Add Phil Elson's percentage histogram example
1 parent 0ee82be commit 0c22d3b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from numpy.random import randn
2+
import matplotlib.pyplot as plt
3+
from matplotlib.ticker import FuncFormatter
4+
5+
def to_percent(y, position):
6+
# Ignore the passed in position. This has the effect of scaling the default
7+
# tick locations.
8+
return str(100 * y)
9+
10+
x = randn(5000)
11+
12+
# Make a normed histogram. It'll be multiplied by 100 later.
13+
plt.hist(x, bins=50, normed=True)
14+
15+
# Create the formatter using the function to_percent. This multiplies all the
16+
# default labels by 100, making them all percentages
17+
formatter = FuncFormatter(to_percent)
18+
19+
# Set the formatter
20+
plt.gca().yaxis.set_major_formatter(formatter)
21+
22+
plt.show()

0 commit comments

Comments
 (0)