|
1 | 1 | """
|
2 | 2 | .. _user_axes_ticks:
|
3 | 3 |
|
4 |
| -========================== |
5 |
| -Tick locations and formats |
6 |
| -========================== |
| 4 | +========== |
| 5 | +Axis Ticks |
| 6 | +========== |
7 | 7 |
|
8 | 8 | The x and y Axis on each Axes have default tick "locators" and "formatters"
|
9 | 9 | that depend on the scale being used (see :ref:`user_axes_scales`). It is
|
@@ -132,3 +132,144 @@ def setup(ax, title):
|
132 | 132 | axs[7].set_xscale('log')
|
133 | 133 | axs[7].xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15))
|
134 | 134 | plt.show()
|
| 135 | + |
| 136 | +# %% |
| 137 | +# |
| 138 | +# Similarly, we can specify "Formatters" for the major and minor ticks on each |
| 139 | +# axis. |
| 140 | +# |
| 141 | +# The tick format is configured via the function `~.Axis.set_major_formatter` |
| 142 | +# or `~.Axis.set_minor_formatter`. It accepts: |
| 143 | +# |
| 144 | +# - a format string, which implicitly creates a `.StrMethodFormatter`. |
| 145 | +# - a function, implicitly creates a `.FuncFormatter`. |
| 146 | +# - an instance of a `.Formatter` subclass. The most common are |
| 147 | +# |
| 148 | +# - `.NullFormatter`: No labels on the ticks. |
| 149 | +# - `.StrMethodFormatter`: Use string `str.format` method. |
| 150 | +# - `.FormatStrFormatter`: Use %-style formatting. |
| 151 | +# - `.FuncFormatter`: Define labels through a function. |
| 152 | +# - `.FixedFormatter`: Set the label strings explicitly. |
| 153 | +# - `.ScalarFormatter`: Default formatter for scalars: auto-pick the format string. |
| 154 | +# - `.PercentFormatter`: Format labels as a percentage. |
| 155 | +# |
| 156 | +# See :ref:`formatters` for the complete list. |
| 157 | + |
| 158 | + |
| 159 | +def setup(ax, title): |
| 160 | + """Set up common parameters for the Axes in the example.""" |
| 161 | + # only show the bottom spine |
| 162 | + ax.yaxis.set_major_locator(ticker.NullLocator()) |
| 163 | + ax.spines[['left', 'right', 'top']].set_visible(False) |
| 164 | + |
| 165 | + # define tick positions |
| 166 | + ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) |
| 167 | + ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) |
| 168 | + |
| 169 | + ax.xaxis.set_ticks_position('bottom') |
| 170 | + ax.tick_params(which='major', width=1.00, length=5) |
| 171 | + ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10) |
| 172 | + ax.set_xlim(0, 5) |
| 173 | + ax.set_ylim(0, 1) |
| 174 | + ax.text(0.0, 0.2, title, transform=ax.transAxes, |
| 175 | + fontsize=14, fontname='Monospace', color='tab:blue') |
| 176 | + |
| 177 | + |
| 178 | +fig = plt.figure(figsize=(8, 8), layout='constrained') |
| 179 | +fig0, fig1, fig2 = fig.subfigures(3, height_ratios=[1.5, 1.5, 7.5]) |
| 180 | + |
| 181 | +fig0.suptitle('String Formatting', fontsize=16, x=0, ha='left') |
| 182 | +ax0 = fig0.subplots() |
| 183 | + |
| 184 | +setup(ax0, title="'{x} km'") |
| 185 | +ax0.xaxis.set_major_formatter('{x} km') |
| 186 | + |
| 187 | +fig1.suptitle('Function Formatting', fontsize=16, x=0, ha='left') |
| 188 | +ax1 = fig1.subplots() |
| 189 | + |
| 190 | +setup(ax1, title="def(x, pos): return str(x-5)") |
| 191 | +ax1.xaxis.set_major_formatter(lambda x, pos: str(x-5)) |
| 192 | + |
| 193 | +fig2.suptitle('Formatter Object Formatting', fontsize=16, x=0, ha='left') |
| 194 | +axs2 = fig2.subplots(7, 1) |
| 195 | + |
| 196 | +setup(axs2[0], title="NullFormatter()") |
| 197 | +axs2[0].xaxis.set_major_formatter(ticker.NullFormatter()) |
| 198 | + |
| 199 | +setup(axs2[1], title="StrMethodFormatter('{x:.3f}')") |
| 200 | +axs2[1].xaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.3f}")) |
| 201 | + |
| 202 | +setup(axs2[2], title="FormatStrFormatter('#%d')") |
| 203 | +axs2[2].xaxis.set_major_formatter(ticker.FormatStrFormatter("#%d")) |
| 204 | + |
| 205 | + |
| 206 | +def fmt_two_digits(x, pos): |
| 207 | + return f'[{x:.2f}]' |
| 208 | + |
| 209 | + |
| 210 | +setup(axs2[3], title='FuncFormatter("[{:.2f}]".format)') |
| 211 | +axs2[3].xaxis.set_major_formatter(ticker.FuncFormatter(fmt_two_digits)) |
| 212 | + |
| 213 | +setup(axs2[4], title="FixedFormatter(['A', 'B', 'C', 'D', 'E', 'F'])") |
| 214 | +# FixedFormatte
B41A
r should only be used together with FixedLocator. |
| 215 | +# Otherwise, one cannot be sure where the labels will end up. |
| 216 | +positions = [0, 1, 2, 3, 4, 5] |
| 217 | +labels = ['A', 'B', 'C', 'D', 'E', 'F'] |
| 218 | +axs2[4].xaxis.set_major_locator(ticker.FixedLocator(positions)) |
| 219 | +axs2[4].xaxis.set_major_formatter(ticker.FixedFormatter(labels)) |
| 220 | + |
| 221 | +setup(axs2[5], title="ScalarFormatter()") |
| 222 | +axs2[5].xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) |
| 223 | + |
| 224 | +setup(axs2[6], title="PercentFormatter(xmax=5)") |
| 225 | +axs2[6].xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5)) |
| 226 | + |
| 227 | + |
| 228 | +# %% |
| 229 | +# |
| 230 | +# Styling ticks (tick parameters) |
| 231 | +# =============================== |
| 232 | +# |
| 233 | +# The appearance of ticks can be controlled at a low level by finding the |
| 234 | +# individual `~.ticker.Ticks` on the axis. However, usually it is simplest to |
| 235 | +# use `~.axes.Axes.tick_params` to change all the objects at once. |
| 236 | +# |
| 237 | +# The ``tick_params`` method can change the properties of ticks: |
| 238 | +# |
| 239 | +# - length |
| 240 | +# - direction (in or out of the frame) |
| 241 | +# - colors |
| 242 | +# - width and length |
| 243 | +# - and whether the ticks are drawn at the bottom, top, left, or right of the |
| 244 | +# Axes. |
| 245 | +# |
| 246 | +# It also can control the tick labels: |
| 247 | +# |
| 248 | +# - labelsize (fontsize) |
| 249 | +# - labelcolor (color of the label) |
| 250 | +# - labelrotation |
| 251 | +# - labelbottom, labeltop, labelleft, labelright |
| 252 | +# |
| 253 | +# In addition there is a *pad* keyword argument that specifies how far the tick |
| 254 | +# label is from the tick. |
| 255 | +# |
| 256 | +# Finally, the grid linestyles can be set: |
| 257 | +# |
| 258 | +# - grid_color |
| 259 | +# - grid_alpha |
| 260 | +# - grid_linewidth |
| 261 | +# - grid_linestyle |
| 262 | +# |
| 263 | +# All these properties can be restricted to one axis, and can be applied to |
| 264 | +# just the major or minor ticks |
| 265 | + |
| 266 | +fig, axs = plt.subplots(1, 2, figsize=(6.4, 3.2), layout='constrained') |
| 267 | + |
| 268 | +for nn, ax in enumerate(axs): |
| 269 | + ax.plot(np.arange(100)) |
| 270 | + if nn == 1: |
| 271 | + ax.grid('on') |
| 272 | + ax.tick_params(right=True, left=False, axis='y', color='r', length=16, |
| 273 | + grid_color='none') |
| 274 | + ax.tick_params(axis='x', color='m', length=4, direction='in', width=4, |
| 275 | + labelcolor='g', grid_color='b') |
0 commit comments