|
3 | 3 | Tick locators |
4 | 4 | ============= |
5 | 5 |
|
6 | | -Show the different tick locators. |
| 6 | +Tick locators define the position of the ticks. |
| 7 | +
|
| 8 | +This example illustrates the usage and effect of the most common locators. |
7 | 9 | """ |
8 | 10 |
|
9 | 11 | import numpy as np |
10 | 12 | import matplotlib.pyplot as plt |
11 | 13 | import matplotlib.ticker as ticker |
12 | 14 |
|
13 | 15 |
|
14 | | -# Setup a plot such that only the bottom spine is shown |
15 | | -def setup(ax): |
| 16 | +def setup(ax, title): |
| 17 | + """Set up common parameters for the Axes in the example.""" |
| 18 | + # only show the bottom spine |
| 19 | + ax.yaxis.set_major_locator(ticker.NullLocator()) |
16 | 20 | ax.spines['right'].set_color('none') |
17 | 21 | ax.spines['left'].set_color('none') |
18 | | - ax.yaxis.set_major_locator(ticker.NullLocator()) |
19 | 22 | ax.spines['top'].set_color('none') |
| 23 | + |
20 | 24 | ax.xaxis.set_ticks_position('bottom') |
21 | | - ax.tick_params(which='major', width=1.00) |
22 | | - ax.tick_params(which='major', length=5) |
23 | | - ax.tick_params(which='minor', width=0.75) |
24 | | - ax.tick_params(which='minor', length=2.5) |
| 25 | + ax.tick_params(which='major', width=1.00, length=5) |
| 26 | + ax.tick_params(which='major', ) |
| 27 | + ax.tick_params(which='minor', width=0.75, length=2.5) |
25 | 28 | ax.set_xlim(0, 5) |
26 | 29 | ax.set_ylim(0, 1) |
27 | | - ax.patch.set_alpha(0.0) |
| 30 | + ax.text(0.0, 0.2, title, transform=ax.transAxes, |
| 31 | + fontsize=14, fontname='Monospace', color='tab:blue') |
28 | 32 |
|
29 | 33 |
|
30 | | -plt.figure(figsize=(8, 6)) |
31 | | -n = 8 |
| 34 | +fig, axs = plt.subplots(8, 1, figsize=(8, 6)) |
32 | 35 |
|
33 | 36 | # Null Locator |
34 | | -ax = plt.subplot(n, 1, 1) |
35 | | -setup(ax) |
36 | | -ax.xaxis.set_major_locator(ticker.NullLocator()) |
37 | | -ax.xaxis.set_minor_locator(ticker.NullLocator()) |
38 | | -ax.text(0.0, 0.1, "NullLocator()", fontsize=14, transform=ax.transAxes) |
| 37 | +setup(axs[0], title="NullLocator()") |
| 38 | +axs[0].xaxis.set_major_locator(ticker.NullLocator()) |
| 39 | +axs[0].xaxis.set_minor_locator(ticker.NullLocator()) |
39 | 40 |
|
40 | 41 | # Multiple Locator |
41 | | -ax = plt.subplot(n, 1, 2) |
42 | | -setup(ax) |
43 | | -ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) |
44 | | -ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) |
45 | | -ax.text(0.0, 0.1, "MultipleLocator(0.5)", fontsize=14, |
46 | | - transform=ax.transAxes) |
| 42 | +setup(axs[1], title="MultipleLocator(0.5)") |
| 43 | +axs[1].xaxis.set_major_locator(ticker.MultipleLocator(0.5)) |
| 44 | +axs[1].xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) |
47 | 45 |
|
48 | 46 | # Fixed Locator |
49 | | -ax = plt.subplot(n, 1, 3) |
50 | | -setup(ax) |
51 | | -majors = [0, 1, 5] |
52 | | -ax.xaxis.set_major_locator(ticker.FixedLocator(majors)) |
53 | | -minors = np.linspace(0, 1, 11)[1:-1] |
54 | | -ax.xaxis.set_minor_locator(ticker.FixedLocator(minors)) |
55 | | -ax.text(0.0, 0.1, "FixedLocator([0, 1, 5])", fontsize=14, |
56 | | - transform=ax.transAxes) |
| 47 | +setup(axs[2], title="FixedLocator([0, 1, 5])") |
| 48 | +axs[2].xaxis.set_major_locator(ticker.FixedLocator([0, 1, 5])) |
| 49 | +axs[2].xaxis.set_minor_locator(ticker.FixedLocator(np.linspace(0.2, 0.8, 4))) |
57 | 50 |
|
58 | 51 | # Linear Locator |
59 | | -ax = plt.subplot(n, 1, 4) |
60 | | -setup(ax) |
61 | | -ax.xaxis.set_major_locator(ticker.LinearLocator(3)) |
62 | | -ax.xaxis.set_minor_locator(ticker.LinearLocator(31)) |
63 | | -ax.text(0.0, 0.1, "LinearLocator(numticks=3)", |
64 | | - fontsize=14, transform=ax.transAxes) |
| 52 | +setup(axs[3], title="LinearLocator(numticks=3)") |
| 53 | +axs[3].xaxis.set_major_locator(ticker.LinearLocator(3)) |
| 54 | +axs[3].xaxis.set_minor_locator(ticker.LinearLocator(31)) |
65 | 55 |
|
66 | 56 | # Index Locator |
67 | | -ax = plt.subplot(n, 1, 5) |
68 | | -setup(ax) |
69 | | -ax.plot(range(0, 5), [0]*5, color='white') |
70 | | -ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25)) |
71 | | -ax.text(0.0, 0.1, "IndexLocator(base=0.5, offset=0.25)", |
72 | | - fontsize=14, transform=ax.transAxes) |
| 57 | +setup(axs[4], title="IndexLocator(base=0.5, offset=0.25)") |
| 58 | +axs[4].plot(range(0, 5), [0]*5, color='white') |
| 59 | +axs[4].xaxis.set_major_locator(ticker.IndexLocator(base=0.5, offset=0.25)) |
73 | 60 |
|
74 | 61 | # Auto Locator |
75 | | -ax = plt.subplot(n, 1, 6) |
76 | | -setup(ax) |
77 | | -ax.xaxis.set_major_locator(ticker.AutoLocator()) |
78 | | -ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) |
79 | | -ax.text(0.0, 0.1, "AutoLocator()", fontsize=14, transform=ax.transAxes) |
| 62 | +setup(axs[5], title="AutoLocator()") |
| 63 | +axs[5].xaxis.set_major_locator(ticker.AutoLocator()) |
| 64 | +axs[5].xaxis.set_minor_locator(ticker.AutoMinorLocator()) |
80 | 65 |
|
81 | 66 | # MaxN Locator |
82 | | -ax = plt.subplot(n, 1, 7) |
83 | | -setup(ax) |
84 | | -ax.xaxis.set_major_locator(ticker.MaxNLocator(4)) |
85 | | -ax.xaxis.set_minor_locator(ticker.MaxNLocator(40)) |
86 | | -ax.text(0.0, 0.1, "MaxNLocator(n=4)", fontsize=14, transform=ax.transAxes) |
| 67 | +setup(axs[6], title="MaxNLocator(n=4)") |
| 68 | +axs[6].xaxis.set_major_locator(ticker.MaxNLocator(4)) |
| 69 | +axs[6].xaxis.set_minor_locator(ticker.MaxNLocator(40)) |
87 | 70 |
|
88 | 71 | # Log Locator |
89 | | -ax = plt.subplot(n, 1, 8) |
90 | | -setup(ax) |
91 | | -ax.set_xlim(10**3, 10**10) |
92 | | -ax.set_xscale('log') |
93 | | -ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15)) |
94 | | -ax.text(0.0, 0.1, "LogLocator(base=10, numticks=15)", |
95 | | - fontsize=15, transform=ax.transAxes) |
96 | | - |
97 | | -# Push the top of the top axes outside the figure because we only show the |
98 | | -# bottom spine. |
99 | | -plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05) |
| 72 | +setup(axs[7], title="LogLocator(base=10, numticks=15)") |
| 73 | +axs[7].set_xlim(10**3, 10**10) |
| 74 | +axs[7].set_xscale('log') |
| 75 | +axs[7].xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15)) |
100 | 76 |
|
| 77 | +plt.tight_layout() |
101 | 78 | plt.show() |
0 commit comments