diff --git a/examples/ticks_and_spines/tick-locators.py b/examples/ticks_and_spines/tick-locators.py index 7586739558a7..373d3a913dd6 100644 --- a/examples/ticks_and_spines/tick-locators.py +++ b/examples/ticks_and_spines/tick-locators.py @@ -3,7 +3,9 @@ Tick locators ============= -Show the different tick locators. +Tick locators define the position of the ticks. + +This example illustrates the usage and effect of the most common locators. """ import numpy as np @@ -11,91 +13,66 @@ import matplotlib.ticker as ticker -# Setup a plot such that only the bottom spine is shown -def setup(ax): +def setup(ax, title): + """Set up common parameters for the Axes in the example.""" + # only show the bottom spine + ax.yaxis.set_major_locator(ticker.NullLocator()) ax.spines['right'].set_color('none') ax.spines['left'].set_color('none') - ax.yaxis.set_major_locator(ticker.NullLocator()) ax.spines['top'].set_color('none') + ax.xaxis.set_ticks_position('bottom') - ax.tick_params(which='major', width=1.00) - ax.tick_params(which='major', length=5) - ax.tick_params(which='minor', width=0.75) - ax.tick_params(which='minor', length=2.5) + ax.tick_params(which='major', width=1.00, length=5) + ax.tick_params(which='major', ) + ax.tick_params(which='minor', width=0.75, length=2.5) ax.set_xlim(0, 5) ax.set_ylim(0, 1) - ax.patch.set_alpha(0.0) + ax.text(0.0, 0.2, title, transform=ax.transAxes, + fontsize=14, fontname='Monospace', color='tab:blue') -plt.figure(figsize=(8, 6)) -n = 8 +fig, axs = plt.subplots(8, 1, figsize=(8, 6)) # Null Locator -ax = plt.subplot(n, 1, 1) -setup(ax) -ax.xaxis.set_major_locator(ticker.NullLocator()) -ax.xaxis.set_minor_locator(ticker.NullLocator()) -ax.text(0.0, 0.1, "NullLocator()", fontsize=14, transform=ax.transAxes) +setup(axs[0], title="NullLocator()") +axs[0].xaxis.set_major_locator(ticker.NullLocator()) +axs[0].xaxis.set_minor_locator(ticker.NullLocator()) # Multiple Locator -ax = plt.subplot(n, 1, 2) -setup(ax) -ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) -ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) -ax.text(0.0, 0.1, "MultipleLocator(0.5)", fontsize=14, - transform=ax.transAxes) +setup(axs[1], title="MultipleLocator(0.5)") +axs[1].xaxis.set_major_locator(ticker.MultipleLocator(0.5)) +axs[1].xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) # Fixed Locator -ax = plt.subplot(n, 1, 3) -setup(ax) -majors = [0, 1, 5] -ax.xaxis.set_major_locator(ticker.FixedLocator(majors)) -minors = np.linspace(0, 1, 11)[1:-1] -ax.xaxis.set_minor_locator(ticker.FixedLocator(minors)) -ax.text(0.0, 0.1, "FixedLocator([0, 1, 5])", fontsize=14, - transform=ax.transAxes) +setup(axs[2], title="FixedLocator([0, 1, 5])") +axs[2].xaxis.set_major_locator(ticker.FixedLocator([0, 1, 5])) +axs[2].xaxis.set_minor_locator(ticker.FixedLocator(np.linspace(0.2, 0.8, 4))) # Linear Locator -ax = plt.subplot(n, 1, 4) -setup(ax) -ax.xaxis.set_major_locator(ticker.LinearLocator(3)) -ax.xaxis.set_minor_locator(ticker.LinearLocator(31)) -ax.text(0.0, 0.1, "LinearLocator(numticks=3)", - fontsize=14, transform=ax.transAxes) +setup(axs[3], title="LinearLocator(numticks=3)") +axs[3].xaxis.set_major_locator(ticker.LinearLocator(3)) +axs[3].xaxis.set_minor_locator(ticker.LinearLocator(31)) # Index Locator -ax = plt.subplot(n, 1, 5) -setup(ax) -ax.plot(range(0, 5), [0]*5, color='white') -ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25)) -ax.text(0.0, 0.1, "IndexLocator(base=0.5, offset=0.25)", - fontsize=14, transform=ax.transAxes) +setup(axs[4], title="IndexLocator(base=0.5, offset=0.25)") +axs[4].plot(range(0, 5), [0]*5, color='white') +axs[4].xaxis.set_major_locator(ticker.IndexLocator(base=0.5, offset=0.25)) # Auto Locator -ax = plt.subplot(n, 1, 6) -setup(ax) -ax.xaxis.set_major_locator(ticker.AutoLocator()) -ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) -ax.text(0.0, 0.1, "AutoLocator()", fontsize=14, transform=ax.transAxes) +setup(axs[5], title="AutoLocator()") +axs[5].xaxis.set_major_locator(ticker.AutoLocator()) +axs[5].xaxis.set_minor_locator(ticker.AutoMinorLocator()) # MaxN Locator -ax = plt.subplot(n, 1, 7) -setup(ax) -ax.xaxis.set_major_locator(ticker.MaxNLocator(4)) -ax.xaxis.set_minor_locator(ticker.MaxNLocator(40)) -ax.text(0.0, 0.1, "MaxNLocator(n=4)", fontsize=14, transform=ax.transAxes) +setup(axs[6], title="MaxNLocator(n=4)") +axs[6].xaxis.set_major_locator(ticker.MaxNLocator(4)) +axs[6].xaxis.set_minor_locator(ticker.MaxNLocator(40)) # Log Locator -ax = plt.subplot(n, 1, 8) -setup(ax) -ax.set_xlim(10**3, 10**10) -ax.set_xscale('log') -ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15)) -ax.text(0.0, 0.1, "LogLocator(base=10, numticks=15)", - fontsize=15, transform=ax.transAxes) - -# Push the top of the top axes outside the figure because we only show the -# bottom spine. -plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05) +setup(axs[7], title="LogLocator(base=10, numticks=15)") +axs[7].set_xlim(10**3, 10**10) +axs[7].set_xscale('log') +axs[7].xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15)) +plt.tight_layout() plt.show()