8000 Backport PR #14088: Cleanup major_minor_demo. · matplotlib/matplotlib@b3bff56 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3bff56

Browse files
timhoffmMeeseeksDev[bot]
authored andcommitted
Backport PR #14088: Cleanup major_minor_demo.
1 parent 8defeda commit b3bff56

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed

examples/ticks_and_spines/major_minor_demo.py

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,73 @@
1-
"""
2-
================
3-
Major Minor Demo
4-
================
1+
r"""
2+
=====================
3+
Major and minor ticks
4+
=====================
55
66
Demonstrate how to use major and minor tickers.
77
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.
1611
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.
2015
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.
2419
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 ::
2823
2924
ax.xaxis.grid(True, which='minor')
3025
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).
3428
"""
3529

3630
import matplotlib.pyplot as plt
3731
import numpy as np
3832
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
3933
AutoMinorLocator)
4034

41-
majorLocator = MultipleLocator(20)
42-
majorFormatter = FormatStrFormatter('%d')
43-
minorLocator = MultipleLocator(5)
44-
4535

4636
t = np.arange(0.0, 100.0, 0.1)
4737
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
4838

4939
fig, ax = plt.subplots()
5040
ax.plot(t, s)
5141

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'))
5447

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))
5750

5851
plt.show()
5952

6053
###############################################################################
6154
# Automatic tick selection for major and minor ticks.
6255
#
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.
6659
#
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.
7463

7564
t = np.arange(0.0, 100.0, 0.01)
7665
s = np.sin(2 * np.pi * t) * np.exp(-t * 0.01)
7766

7867
fig, ax = plt.subplots()
7968
ax.plot(t, s)
8069

81-
ax.xaxis.set_minor_locator(minorLocator)
70+
ax.xaxis.set_minor_locator(AutoMinorLocator())
8271

8372
ax.tick_params(which='both', width=2)
8473
ax.tick_params(which='major', length=7)

0 commit comments

Comments
 (0)
0