Skip to content

Commit 2b5e044

Browse files
jklymakMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #15453: Improve example for tick locators
1 parent b7c925e commit 2b5e044

File tree

1 file changed

+40
-63
lines changed

1 file changed

+40
-63
lines changed

examples/ticks_and_spines/tick-locators.py

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,76 @@
33
Tick locators
44
=============
55
6-
Show the different tick locators.
6+
Tick locators define the position of the ticks.
7+
8+
This example illustrates the usage and effect of the most common locators.
79
"""
810

911
import numpy as np
1012
import matplotlib.pyplot as plt
1113
import matplotlib.ticker as ticker
1214

1315

14-
# Setup a plot such that only the bottom spine is shown
15-
def setup(ax):
16+
def setup(ax, title):
17+
"""Set up common parameters for the Axes in the example."""
18+
# only show the bottom spine
19+
ax.yaxis.set_major_locator(ticker.NullLocator())
1620
ax.spines['right'].set_color('none')
1721
ax.spines['left'].set_color('none')
18-
ax.yaxis.set_major_locator(ticker.NullLocator())
1922
ax.spines['top'].set_color('none')
23+
2024
ax.xaxis.set_ticks_position('bottom')
21-
ax.tick_params(which='major', width=1.00)
22-
ax.tick_params(which='major', length=5)
23-
ax.tick_params(which='minor', width=0.75)
24-
ax.tick_params(which='minor', length=2.5)
25+
ax.tick_params(which='major', width=1.00, length=5)
26+
ax.tick_params(which='major', )
27+
ax.tick_params(which='minor', width=0.75, length=2.5)
2528
ax.set_xlim(0, 5)
2629
ax.set_ylim(0, 1)
27-
ax.patch.set_alpha(0.0)
30+
ax.text(0.0, 0.2, title, transform=ax.transAxes,
31+
fontsize=14, fontname='Monospace', color='tab:blue')
2832

2933

30-
plt.figure(figsize=(8, 6))
31-
n = 8
34+
fig, axs = plt.subplots(8, 1, figsize=(8, 6))
3235

3336
# Null Locator
34-
ax = plt.subplot(n, 1, 1)
35-
setup(ax)
36-
ax.xaxis.set_major_locator(ticker.NullLocator())
37-
ax.xaxis.set_minor_locator(ticker.NullLocator())
38-
ax.text(0.0, 0.1, "NullLocator()", fontsize=14, transform=ax.transAxes)
37+
setup(axs[0], title="NullLocator()")
38+
axs[0].xaxis.set_major_locator(ticker.NullLocator())
39+
axs[0].xaxis.set_minor_locator(ticker.NullLocator())
3940

4041
# Multiple Locator
41-
ax = plt.subplot(n, 1, 2)
42-
setup(ax)
43-
ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5))
44-
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1))
45-
ax.text(0.0, 0.1, "MultipleLocator(0.5)", fontsize=14,
46-
transform=ax.transAxes)
42+
setup(axs[1], title="MultipleLocator(0.5)")
43+
axs[1].xaxis.set_major_locator(ticker.MultipleLocator(0.5))
44+
axs[1].xaxis.set_minor_locator(ticker.MultipleLocator(0.1))
4745

4846
# Fixed Locator
49-
ax = plt.subplot(n, 1, 3)
50-
setup(ax)
51-
majors = [0, 1, 5]
52-
ax.xaxis.set_major_locator(ticker.FixedLocator(majors))
53-
minors = np.linspace(0, 1, 11)[1:-1]
54-
ax.xaxis.set_minor_locator(ticker.FixedLocator(minors))
55-
ax.text(0.0, 0.1, "FixedLocator([0, 1, 5])", fontsize=14,
56-
transform=ax.transAxes)
47+
setup(axs[2], title="FixedLocator([0, 1, 5])")
48+
axs[2].xaxis.set_major_locator(ticker.FixedLocator([0, 1, 5]))
49+
axs[2].xaxis.set_minor_locator(ticker.FixedLocator(np.linspace(0.2, 0.8, 4)))
5750

5851
# Linear Locator
59-
ax = plt.subplot(n, 1, 4)
60-
setup(ax)
61-
ax.xaxis.set_major_locator(ticker.LinearLocator(3))
62-
ax.xaxis.set_minor_locator(ticker.LinearLocator(31))
63-
ax.text(0.0, 0.1, "LinearLocator(numticks=3)",
64-
fontsize=14, transform=ax.transAxes)
52+
setup(axs[3], title="LinearLocator(numticks=3)")
53+
axs[3].xaxis.set_major_locator(ticker.LinearLocator(3))
54+
axs[3].xaxis.set_minor_locator(ticker.LinearLocator(31))
6555

6656
# Index Locator
67-
ax = plt.subplot(n, 1, 5)
68-
setup(ax)
69-
ax.plot(range(0, 5), [0]*5, color='white')
70-
ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25))
71-
ax.text(0.0, 0.1, "IndexLocator(base=0.5, offset=0.25)",
72-
fontsize=14, transform=ax.transAxes)
57+
setup(axs[4], title="IndexLocator(base=0.5, offset=0.25)")
58+
axs[4].plot(range(0, 5), [0]*5, color='white')
59+
axs[4].xaxis.set_major_locator(ticker.IndexLocator(base=0.5, offset=0.25))
7360

7461
# Auto Locator
75-
ax = plt.subplot(n, 1, 6)
76-
setup(ax)
77-
ax.xaxis.set_major_locator(ticker.AutoLocator())
78-
ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
79-
ax.text(0.0, 0.1, "AutoLocator()", fontsize=14, transform=ax.transAxes)
62+
setup(axs[5], title="AutoLocator()")
63+
axs[5].xaxis.set_major_locator(ticker.AutoLocator())
64+
axs[5].xaxis.set_minor_locator(ticker.AutoMinorLocator())
8065

8166
# MaxN Locator
82-
ax = plt.subplot(n, 1, 7)
83-
setup(ax)
84-
ax.xaxis.set_major_locator(ticker.MaxNLocator(4))
85-
ax.xaxis.set_minor_locator(ticker.MaxNLocator(40))
86-
ax.text(0.0, 0.1, "MaxNLocator(n=4)", fontsize=14, transform=ax.transAxes)
67+
setup(axs[6], title="MaxNLocator(n=4)")
68+
axs[6].xaxis.set_major_locator(ticker.MaxNLocator(4))
69+
axs[6].xaxis.set_minor_locator(ticker.MaxNLocator(40))
8770

8871
# Log Locator
89-
ax = plt.subplot(n, 1, 8)
90-
setup(ax)
91-
ax.set_xlim(10**3, 10**10)
92-
ax.set_xscale('log')
93-
ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15))
94-
ax.text(0.0, 0.1, "LogLocator(base=10, numticks=15)",
95-
fontsize=15, transform=ax.transAxes)
96-
97-
# Push the top of the top axes outside the figure because we only show the
98-
# bottom spine.
99-
plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05)
72+
setup(axs[7], title="LogLocator(base=10, numticks=15)")
73+
axs[7].set_xlim(10**3, 10**10)
74+
axs[7].set_xscale('log')
75+
axs[7].xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15))
10076

77+
plt.tight_layout()
10178
plt.show()

0 commit comments

Comments
 (0)