File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
examples/ticks_and_spines Expand file tree Collapse file tree 1 file changed +36
-0
lines changed 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: The MaxNLocator is used here to ensure that the tick
13
+ values take integer values. As such, we need to catch
14
+ any IndexErrors in the format function where we have not
15
+ defined a label for that particular tick
16
+
17
+ """
18
+
19
+ import matplotlib .pyplot as plt
20
+ from matplotlib .ticker import FuncFormatter , MaxNLocator
21
+ fig = plt .figure ()
22
+ ax = fig .add_subplot (111 )
23
+ xs = range (26 )
24
+ ys = range (26 )
25
+ labels = list ('abcdefghijklmnopqrstuvwxyz' )
26
+
27
+
28
+ def format_fn (tick_val , tick_pos ):
29
+ if int (tick_val ) in xs :
30
+ return labels [int (tick_val )]
31
+ else :
32
+ return ''
33
+ ax .xaxis .set_major_formatter (FuncFormatter (format_fn ))
34
+ ax .xaxis .set_major_locator (MaxNLocator (integer = True ))
35
+ ax .plot (xs , ys )
36
+ plt .show ()
You can’t perform that action at this time.
0 commit comments