|
| 1 | +""" |
| 2 | +.. _user_axes_ticks: |
| 3 | +
|
| 4 | +========================== |
| 5 | +Tick locations and formats |
| 6 | +========================== |
| 7 | +
|
| 8 | +The x and y Axis on each Axes have default tick "locators" and "formatters" |
| 9 | +that depend on the scale being used (see :ref:`user_axes_scales`). It is |
| 10 | +possible to customize the ticks and tick labels with either high-level methods |
| 11 | +like `~.axes.Axes.set_xticks` or set the locators and formatters directly on |
| 12 | +the axis. |
| 13 | +
|
| 14 | +Manual location and formats |
| 15 | +=========================== |
| 16 | +
|
| 17 | +The simplest method to customize the tick locations and formats is to use |
| 18 | +`~.axes.Axes.set_xticks` and `~.axes.Axes.set_yticks`. These can be used on |
| 19 | +either the major or the minor ticks. |
| 20 | +""" |
| 21 | +import numpy as np |
| 22 | +import matplotlib.pyplot as plt |
| 23 | + |
| 24 | +import matplotlib.ticker as ticker |
| 25 | + |
| 26 | + |
| 27 | +fig, axs = plt.subplots(2, 1, figsize=(5.4, 5.4), layout='constrained') |
| 28 | +x = np.arange(100) |
| 29 | +for nn, ax in enumerate(axs): |
| 30 | + ax.plot(x, x) |
| 31 | + if nn == 1: |
| 32 | + ax.set_title('Manual ticks') |
| 33 | + ax.set_yticks(np.arange(0, 100.1, 100/3)) |
| 34 | + xticks = np.arange(0.50, 101, 20) |
| 35 | + xlabels = [f'\\${x:1.2f}' for x in xticks] |
| 36 | + ax.set_xticks(xticks, labels=xlabels) |
| 37 | + else: |
| 38 | + ax.set_title('Automatic ticks') |
| 39 | + |
| 40 | +# %% |
| 41 | +# |
| 42 | +# Note that the length of the ``labels`` argument must have the same length as |
| 43 | +# the array used to specify the ticks. |
| 44 | +# |
| 45 | +# By default `~.axes.Axes.set_xticks` and `~.axes.Axes.set_yticks` act on the |
| 46 | +# major ticks of an Axis, however it is possible to add minor ticks: |
| 47 | + |
| 48 | +fig, axs = plt.subplots(2, 1, figsize=(5.4, 5.4), layout='constrained') |
| 49 | +x = np.arange(100) |
| 50 | +for nn, ax in enumerate(axs): |
| 51 | + ax.plot(x, x) |
| 52 | + if nn == 1: |
| 53 | + ax.set_title('Manual ticks') |
| 54 | + ax.set_yticks(np.arange(0, 100.1, 100/3)) |
| 55 | + ax.set_yticks(np.arange(0, 100.1, 100/30), minor=True) |
| 56 | + else: |
| 57 | + ax.set_title('Automatic ticks') |
| 58 | + |
| 59 | + |
| 60 | +# %% |
| 61 | +# |
| 62 | +# Locators and Formatters |
| 63 | +# ======================= |
| 64 | +# |
| 65 | +# Manually setting the ticks as above works well for specific final plots, but |
| 66 | +# does not adapt as the user interacts with the axes. At a lower level, |
| 67 | +# Matplotlib has ``Locators`` that are meant to automatically choose ticks |
| 68 | +# depending on the current view limits of the axis, and ``Formatters`` that are |
| 69 | +# meant to format the tick labels automatically. |
| 70 | +# |
| 71 | +# The full list of locators provided by Matplotlib are listed at |
| 72 | +# :ref:`locators`, and the formatters at :ref:`formatters`. |
| 73 | + |
| 74 | + |
| 75 | +# %% |
| 76 | + |
| 77 | +def setup(ax, title): |
| 78 | + """Set up common parameters for the Axes in the example.""" |
| 79 | + # only show the bottom spine |
| 80 | + ax.yaxis.set_major_locator(ticker.NullLocator()) |
| 81 | + ax.spines[['left', 'right', 'top']].set_visible(False) |
| 82 | + |
| 83 | + ax.xaxis.set_ticks_position('bottom') |
| 84 | + ax.tick_params(which='major', width=1.00, length=5) |
| 85 | + ax.tick_params(which='minor', width=0.75, length=2.5) |
| 86 | + ax.set_xlim(0, 5) |
| 87 | + ax.set_ylim(0, 1) |
| 88 | + ax.text(0.0, 0.2, title, transform=ax.transAxes, |
| 89 | + fontsize=14, fontname='Monospace', color='tab:blue') |
| 90 | + |
| 91 | + |
| 92 | +fig, axs = plt.subplots(8, 1, layout='constrained') |
| 93 | + |
| 94 | +# Null Locator |
| 95 | +setup(axs[0], title="NullLocator()") |
| 96 | +axs[0].xaxis.set_major_locator(ticker.NullLocator()) |
| 97 | +axs[0].xaxis.set_minor_locator(ticker.NullLocator()) |
| 98 | + |
| 99 | +# Multiple Locator |
| 100 | +setup(axs[1], title="MultipleLocator(0.5)") |
| 101 | +axs[1].xaxis.set_major_locator(ticker.MultipleLocator(0.5)) |
| 102 | +axs[1].xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) |
| 103 | + |
| 104 | +# Fixed Locator |
| 105 | +setup(axs[2], title="FixedLocator([0, 1, 5])") |
| 106 | +axs[2].xaxis.set_major_locator(ticker.FixedLocator([0, 1, 5])) |
| 107 | +axs[2].xaxis.set_minor_locator(ticker.FixedLocator(np.linspace(0.2, 0.8, 4))) |
| 108 | + |
| 109 | +# Linear Locator |
| 110 | +setup(axs[3], title="LinearLocator(numticks=3)") |
| 111 | +axs[3].xaxis.set_major_locator(ticker.LinearLocator(3)) |
| 112 | +axs[3].xaxis.set_minor_locator(ticker.LinearLocator(31)) |
| 113 | + |
| 114 | +# Index Locator |
| 115 | +setup(axs[4], title="IndexLocator(base=0.5, offset=0.25)") |
| 116 | +axs[4].plot(range(0, 5), [0]*5, color='white') |
| 117 | +axs[4].xaxis.set_major_locator(ticker.IndexLocator(base=0.5, offset=0.25)) |
| 118 | + |
| 119 | +# Auto Locator |
| 120 | +setup(axs[5], title="AutoLocator()") |
| 121 | +axs[5].xaxis.set_major_locator(ticker.AutoLocator()) |
| 122 | +axs[5].xaxis.set_minor_locator(ticker.AutoMinorLocator()) |
| 123 | + |
| 124 | +# MaxN Locator |
| 125 | +setup(axs[6], title="MaxNLocator(n=4)") |
| 126 | +axs[6].xaxis.set_major_locator(ticker.MaxNLocator(4)) |
| 127 | +axs[6].xaxis.set_minor_locator(ticker.MaxNLocator(40)) |
| 128 | + |
| 129 | +# Log Locator |
| 130 | +setup(axs[7], title="LogLocator(base=10, numticks=15)") |
| 131 | +axs[7].set_xlim(10**3, 10**10) |
| 132 | +axs[7].set_xscale('log') |
| 133 | +axs[7].xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15)) |
| 134 | +plt.show() |
0 commit comments