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