|
1 |
| -""" |
2 |
| -================ |
3 |
| -Major Minor Demo |
4 |
| -================ |
| 1 | +r""" |
| 2 | +===================== |
| 3 | +Major and minor ticks |
| 4 | +===================== |
5 | 5 |
|
6 | 6 | Demonstrate how to use major and minor tickers.
|
7 | 7 |
|
8 |
| -The two relevant userland classes are Locators and Formatters. |
9 |
| -Locators determine where the ticks are and formatters control the |
10 |
| -formatting of ticks. |
11 |
| -
|
12 |
| -Minor ticks are off by default (NullLocator and NullFormatter). You |
13 |
| -can turn minor ticks on w/o labels by setting the minor locator. You |
14 |
| -can also turn labeling on for the minor ticker by setting the minor |
15 |
| -formatter |
| 8 | +The two relevant classes are `.Locator`\s and `.Formatter`\s. Locators |
| 9 | +determine where the ticks are, and formatters control the formatting of tick |
| 10 | +labels. |
16 | 11 |
|
17 |
| -Make a plot with major ticks that are multiples of 20 and minor ticks |
18 |
| -that are multiples of 5. Label major ticks with %d formatting but |
19 |
| -don't label minor ticks |
| 12 | +Minor ticks are off by default (using `.NullLocator` and `.NullFormatter`). |
| 13 | +Minor ticks can be turned on without labels by setting the minor locator. |
| 14 | +Minor tick labels can be turned on by setting the minor formatter. |
20 | 15 |
|
21 |
| -The MultipleLocator ticker class is used to place ticks on multiples of |
22 |
| -some base. The FormatStrFormatter uses a string format string (e.g., |
23 |
| -'%d' or '%1.2f' or '%1.1f cm' ) to format the tick |
| 16 | +`MultipleLocator` places ticks on multiples of some base. `FormatStrFormatter` |
| 17 | +uses a format string (e.g., '%d' or '%1.2f' or '%1.1f cm' ) to format the tick |
| 18 | +labels. |
24 | 19 |
|
25 |
| -The pyplot interface grid command changes the grid settings of the |
26 |
| -major ticks of the y and y axis together. If you want to control the |
27 |
| -grid of the minor ticks for a given axis, use for example |
| 20 | +`.pyplot.grid` changes the grid settings of the major ticks of the y and y axis |
| 21 | +together. If you want to control the grid of the minor ticks for a given axis, |
| 22 | +use for example :: |
28 | 23 |
|
29 | 24 | ax.xaxis.grid(True, which='minor')
|
30 | 25 |
|
31 |
| -Note, you should not use the same locator between different Axis
8000
|
32 |
| -because the locator stores references to the Axis data and view limits |
33 |
| -
|
| 26 | +Note that a given locator or formatter instance can only be used on a single |
| 27 | +axis (because the locator stores references to the axis data and view limits). |
34 | 28 | """
|
35 | 29 |
|
36 | 30 | import matplotlib.pyplot as plt
|
37 | 31 | import numpy as np
|
38 | 32 | from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
|
39 | 33 | AutoMinorLocator)
|
40 | 34 |
|
41 |
| -majorLocator = MultipleLocator(20) |
42 |
| -majorFormatter = FormatStrFormatter('%d') |
43 |
| -minorLocator = MultipleLocator(5) |
44 |
| - |
45 | 35 |
|
46 | 36 | t = np.arange(0.0, 100.0, 0.1)
|
47 | 37 | s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
|
48 | 38 |
|
49 | 39 | fig, ax = plt.subplots()
|
50 | 40 | ax.plot(t, s)
|
51 | 41 |
|
52 |
| -ax.xaxis.set_major_locator(majorLocator) |
53 |
| -ax.xaxis.set_major_formatter(majorFormatter) |
| 42 | +# Make a plot with major ticks that are multiples of 20 and minor ticks that |
| 43 | +# are multiples of 5. Label major ticks with '%d' formatting but don't label |
| 44 | +# minor ticks. |
| 45 | +ax.xaxis.set_major_locator(MultipleLocator(20)) |
| 46 | +ax.xaxis.set_major_formatter(FormatStrFormatter('%d')) |
54 | 47 |
|
55 |
| -# for the minor ticks, use no labels; default NullFormatter |
56 |
| -ax.xaxis.set_minor_locator(minorLocator) |
| 48 | +# For the minor ticks, use no labels; default NullFormatter. |
| 49 | +ax.xaxis.set_minor_locator(MultipleLocator(5)) |
57 | 50 |
|
58 | 51 | plt.show()
|
59 | 52 |
|
60 | 53 | ###############################################################################
|
61 | 54 | # Automatic tick selection for major and minor ticks.
|
62 | 55 | #
|
63 |
| -# Use interactive pan and zoom to see how the tick intervals |
64 |
| -# change. There will be either 4 or 5 minor tick intervals |
65 |
| -# per major interval, depending on the major interval. |
| 56 | +# Use interactive pan and zoom to see how the tick intervals change. There will |
| 57 | +# be either 4 or 5 minor tick intervals per major interval, depending on the |
| 58 | +# major interval. |
66 | 59 | #
|
67 |
| -# One can supply an argument to AutoMinorLocator to |
68 |
| -# specify a fixed number of minor intervals per major interval, e.g.: |
69 |
| -# minorLocator = AutoMinorLocator(2) |
70 |
| -# would lead to a single minor tick between major ticks. |
71 |
| - |
72 |
| -minorLocator = AutoMinorLocator() |
73 |
| - |
| 60 | +# One can supply an argument to AutoMinorLocator to specify a fixed number of |
| 61 | +# minor intervals per major interval, e.g. ``AutoMinorLocator(2)`` would lead |
| 62 | +# to a single minor tick between major ticks. |
74 | 63 |
|
75 | 64 | t = np.arange(0.0, 100.0, 0.01)
|
76 | 65 | s = np.sin(2 * np.pi * t) * np.exp(-t * 0.01)
|
77 | 66 |
|
78 | 67 | fig, ax = plt.subplots()
|
79 | 68 | ax.plot(t, s)
|
80 | 69 |
|
81 |
| -ax.xaxis.set_minor_locator(minorLocator) |
| 70 | +ax.xaxis.set_minor_locator(AutoMinorLocator()) |
82 | 71 |
|
83 | 72 | ax.tick_params(which='both', width=2)
|
84 | 73 | ax.tick_params(which='major', length=7)
|
|
0 commit comments