File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments