Skip to content

Commit 1985162

Browse files
committed
Add a percent symbol to tick labels
Got to be careful when latex is involved.
1 parent 0c22d3b commit 1985162

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

examples/pylab_examples/histogram_percent_demo.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
import matplotlib
12
from numpy.random import randn
23
import matplotlib.pyplot as plt
34
from matplotlib.ticker import FuncFormatter
45

56
def to_percent(y, position):
67
# Ignore the passed in position. This has the effect of scaling the default
78
# tick locations.
8-
return str(100 * y)
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 + '%'
916

1017
x = randn(5000)
1118

0 commit comments

Comments
 (0)