|
14 | 14 | Minor tick labels can be turned on by setting the minor formatter.
|
15 | 15 |
|
16 | 16 | `.MultipleLocator` places ticks on multiples of some base.
|
17 |
| -`.FormatStrFormatter` uses a format string (e.g., ``'%d'`` or ``'%1.2f'`` or |
18 |
| -``'%1.1f cm'``) to format the tick labels. |
| 17 | +`.StrMethodFormatter` uses a format string (e.g., ``'{x:d}'`` or ``'{x:1.2f}'`` |
| 18 | +or ``'{x:1.1f} cm'``) to format the tick labels (the variable in the format |
| 19 | +string must be ``'x'``). For a `.StrMethodFormatter`, the string can be passed |
| 20 | +directly to `.Axis.set_major_formatter` or |
| 21 | +`.Axis.set_minor_formatter`. An appropriate `.StrMethodFormatter` will |
| 22 | +be created and used automatically. |
19 | 23 |
|
20 | 24 | `.pyplot.grid` changes the grid settings of the major ticks of the y and y axis
|
21 | 25 | together. If you want to control the grid of the minor ticks for a given axis,
|
|
29 | 33 |
|
30 | 34 | import matplotlib.pyplot as plt
|
31 | 35 | import numpy as np
|
32 |
| -from matplotlib.ticker import (MultipleLocator, FormatStrFormatter, |
33 |
| - AutoMinorLocator) |
| 36 | +from matplotlib.ticker import (MultipleLocator, AutoMinorLocator) |
34 | 37 |
|
35 | 38 |
|
36 | 39 | t = np.arange(0.0, 100.0, 0.1)
|
|
40 | 43 | ax.plot(t, s)
|
41 | 44 |
|
42 | 45 | # 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. |
| 46 | +# are multiples of 5. Label major ticks with '.0f' formatting but don't label |
| 47 | +# minor ticks. The string is used directly, the `StrMethodFormatter` is |
| 48 | +# created automatically. |
45 | 49 | ax.xaxis.set_major_locator(MultipleLocator(20))
|
46 |
| -ax.xaxis.set_major_formatter(FormatStrFormatter('%d')) |
| 50 | +ax.xaxis.set_major_formatter('{x:.0f}') |
47 | 51 |
|
48 | 52 | # For the minor ticks, use no labels; default NullFormatter.
|
49 | 53 | ax.xaxis.set_minor_locator(MultipleLocator(5))
|
|
74 | 78 | ax.tick_params(which='minor', length=4, color='r')
|
75 | 79 |
|
76 | 80 | plt.show()
|
| 81 | + |
| 82 | + |
| 83 | +############################################################################# |
| 84 | +# |
| 85 | +# ------------ |
| 86 | +# |
| 87 | +# References |
| 88 | +# """""""""" |
| 89 | +# |
| 90 | +# The use of the following functions, methods, classes and modules is shown |
| 91 | +# in this example: |
| 92 | + |
| 93 | +import matplotlib |
| 94 | +matplotlib.pyplot.subplots |
| 95 | +matplotlib.axis.Axis.set_major_formatter |
| 96 | +matplotlib.axis.Axis.set_major_locator |
| 97 | +matplotlib.axis.Axis.set_minor_locator |
| 98 | +matplotlib.ticker.AutoMinorLocator |
| 99 | +matplotlib.ticker.MultipleLocator |
| 100 | +matplotlib.ticker.StrMethodFormatter |
0 commit comments