Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47a61d7

Browse files
committed
Merge pull request #1149 from dmcdougall/percent
Add Phil Elson's percentage histogram example
2 parents 1d3faee + 1985162 commit 47a61d7

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)