1"""
2=============
3Tick locators
4=============
5
6Show the different tick locators.
7"""
8
9import numpy as np
10import matplotlib.pyplot as plt
11import matplotlib.ticker as ticker
12
13
14# Setup a plot such that only the bottom spine is shown
15def setup(ax):
16    ax.spines['right'].set_color('none')
17    ax.spines['left'].set_color('none')
18    ax.yaxis.set_major_locator(ticker.NullLocator())
19    ax.spines['top'].set_color('none')
20    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.set_xlim(0, 5)
26    ax.set_ylim(0, 1)
27    ax.patch.set_alpha(0.0)
28
29
30plt.figure(figsize=(8, 6))
31n = 8
32
33# Null Locator
34ax = plt.subplot(n, 1, 1)
35setup(ax)
36ax.xaxis.set_major_locator(ticker.NullLocator())
37ax.xaxis.set_minor_locator(ticker.NullLocator())
38ax.text(0.0, 0.1, "NullLocator()", fontsize=14, transform=ax.transAxes)
39
40# Multiple Locator
41ax = plt.subplot(n, 1, 2)
42setup(ax)
43ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5))
44ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1))
45ax.text(0.0, 0.1, "MultipleLocator(0.5)", fontsize=14,
46        transform=ax.transAxes)
47
48# Fixed Locator
49ax = plt.subplot(n, 1, 3)
50setup(ax)
51majors = [0, 1, 5]
52ax.xaxis.set_major_locator(ticker.FixedLocator(majors))
53minors = np.linspace(0, 1, 11)[1:-1]
54ax.xaxis.set_minor_locator(ticker.FixedLocator(minors))
55ax.text(0.0, 0.1, "FixedLocator([0, 1, 5])", fontsize=14,
56        transform=ax.transAxes)
57
58# Linear Locator
59ax = plt.subplot(n, 1, 4)
60setup(ax)
61ax.xaxis.set_major_locator(ticker.LinearLocator(3))
62ax.xaxis.set_minor_locator(ticker.LinearLocator(31))
63ax.text(0.0, 0.1, "LinearLocator(numticks=3)",
64        fontsize=14, transform=ax.transAxes)
65
66# Index Locator
67ax = plt.subplot(n, 1, 5)
68setup(ax)
69ax.plot(range(0, 5), [0]*5, color='White')
70ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25))
71ax.text(0.0, 0.1, "IndexLocator(base=0.5, offset=0.25)",
72        fontsize=14, transform=ax.transAxes)
73
74# Auto Locator
75ax = plt.subplot(n, 1, 6)
76setup(ax)
77ax.xaxis.set_major_locator(ticker.AutoLocator())
78ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
79ax.text(0.0, 0.1, "AutoLocator()", fontsize=14, transform=ax.transAxes)
80
81# MaxN Locator
82ax = plt.subplot(n, 1, 7)
83setup(ax)
84ax.xaxis.set_major_locator(ticker.MaxNLocator(4))
85ax.xaxis.set_minor_locator(ticker.MaxNLocator(40))
86ax.text(0.0, 0.1, "MaxNLocator(n=4)", fontsize=14, transform=ax.transAxes)
87
88# Log Locator
89ax = plt.subplot(n, 1, 8)
90setup(ax)
91ax.set_xlim(10**3, 10**10)
92ax.set_xscale('log')
93ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15))
94ax.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.
99plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05)
100
101plt.show()
102