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

Skip to content

Commit 9cba700

Browse files
committed
added tick labels from values demo
1 parent b9bc7d1 commit 9cba700

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
3+
Basic demo showing how to set tick labels to values of a series.
4+
5+
Using ax.set_xticks causes the tick labels to be set on the currently
6+
chosen ticks. However, you may want to allow matplotlib to dynamically
7+
choose the number of ticks and their spacing.
8+
9+
In this case may be better to determine the tick label from the value
10+
at the tick. The following example shows how to do this.
11+
12+
NB: You may want to combine the solution below with
13+
`xaxis.set_major_locator(MaxNLocator(integer=True))` to ensure
14+
that tick values are always at integer values, and therefore always use
15+
the appropriate label.
16+
17+
"""
18+
19+
20+
21+
import matplotlib.pyplot as plt
22+
from matplotlib.ticker import FuncFormatter
23+
24+
fig = plt.figure()
25+
ax = fig.add_subplot(111)
26+
xs = range(26)
27+
ys = range(26)
28+
labels = list('abcdefghijklmnopqrstuvwxyz')
29+
30+
def format_fn(tick_val, tick_pos):
31+
return labels[int(tick_val)]
32+
33+
ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
34+
35+
ax.plot(xs, ys)
36+
plt.show()

0 commit comments

Comments
 (0)