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