|
3 | 3 | # Author: Nicolas P. Rougier
|
4 | 4 | # License: BSD
|
5 | 5 | # ----------------------------------------------------------------------------
|
6 |
| -import pathlib |
7 |
| - |
8 |
| -import numpy as np |
9 |
| -import matplotlib as mpl |
10 | 6 | import matplotlib.pyplot as plt
|
11 | 7 | import matplotlib.ticker as ticker
|
12 | 8 |
|
| 9 | +# Setup a plot such that only the bottom spine is shown |
| 10 | + |
13 | 11 |
|
14 |
| -mpl.style.use([ |
15 |
| - pathlib.Path(__file__).parent/'../styles/base.mplstyle', |
16 |
| - pathlib.Path(__file__).parent/'../styles/ticks.mplstyle', |
17 |
| -]) |
| 12 | +def setup(ax): |
| 13 | + """Set up Axes with just an x-Axis.""" |
| 14 | + ax.spines['right'].set_color('none') |
| 15 | + ax.spines['left'].set_color('none') |
| 16 | + ax.yaxis.set_major_locator(ticker.NullLocator()) |
| 17 | + ax.spines['top'].set_color('none') |
| 18 | + ax.xaxis.set_ticks_position('bottom') |
| 19 | + ax.tick_params(which='major', width=1.00, length=5) |
| 20 | + ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10) |
| 21 | + ax.set_xlim(0, 5) |
| 22 | + ax.set_ylim(0, 1) |
| 23 | + ax.patch.set_alpha(0.0) |
18 | 24 |
|
19 | 25 |
|
20 |
| -subplots_kw = dict( |
21 |
| - figsize=(5.7/2.54, 3.5/2.54), |
22 |
| - nrows=7, |
23 |
| - subplot_kw=dict(xlim=(0, 5), ylim=(0, 1)) |
24 |
| -) |
| 26 | +fig = plt.figure(figsize=(8, 5)) |
| 27 | +fig.patch.set_alpha(0.0) |
| 28 | +n = 7 |
25 | 29 |
|
26 |
| -(fig, axs) = plt.subplots(**subplots_kw) |
| 30 | +fontsize = 18 |
| 31 | +family = "Source Code Pro" |
27 | 32 |
|
28 | 33 | # Null formatter
|
29 |
| -ax = axs[0] |
| 34 | +ax = fig.add_subplot(n, 1, 1) |
| 35 | +setup(ax) |
30 | 36 | ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
|
31 | 37 | ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
|
32 | 38 | ax.xaxis.set_major_formatter(ticker.NullFormatter())
|
33 | 39 | ax.xaxis.set_minor_formatter(ticker.NullFormatter())
|
34 |
| -ax.text(0.0, 0.2, "ticker.NullFormatter()", |
35 |
| - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) |
| 40 | +ax.text(0.0, 0.1, "ticker.NullFormatter()", family=family, |
| 41 | + fontsize=fontsize, transform=ax.transAxes) |
36 | 42 |
|
37 | 43 | # Fixed formatter
|
38 |
| -ax = axs[1] |
| 44 | +ax = fig.add_subplot(n, 1, 2) |
| 45 | +setup(ax) |
39 | 46 | ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
|
40 | 47 | ax.xaxis.set_major_locator(ticker.FixedLocator(range(6)))
|
41 | 48 | majors = ["zero", "one", "two", "three", "four", "five"]
|
42 | 49 | ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors))
|
43 |
| -ax.text(0.0, 0.2, "ticker.FixedFormatter(['zero', 'one', 'two', …])", |
44 |
| - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) |
| 50 | +ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", |
| 51 | + family=family, fontsize=fontsize, transform=ax.transAxes) |
| 52 | + |
| 53 | + |
| 54 | +# FuncFormatter can be used as a decorator |
| 55 | +@ticker.FuncFormatter |
| 56 | +def major_formatter(x, pos): |
| 57 | + """Return formatted value with 2 decimal places.""" |
| 58 | + return "[%.2f]" % x |
45 | 59 |
|
46 |
| -# FuncFormatter formatter |
47 |
| -ax = axs[2] |
| 60 | + |
| 61 | +ax = fig.add_subplot(n, 1, 3) |
| 62 | +setup(ax) |
48 | 63 | ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
|
49 | 64 | ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
|
50 |
| -ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)) |
51 |
| -ax.text(0.0, 0.2, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', |
52 |
| - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) |
| 65 | +ax.xaxis.set_major_formatter(major_formatter) |
| 66 | +ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', |
| 67 | + family=family, fontsize=fontsize, transform=ax.transAxes) |
| 68 | + |
53 | 69 |
|
54 | 70 | # FormatStr formatter
|
55 |
| -ax = axs[3] |
| 71 | +ax = fig.add_subplot(n, 1, 4) |
| 72 | +setup(ax) |
56 | 73 | ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
|
57 | 74 | ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
|
58 | 75 | ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<"))
|
59 |
| -ax.text(0.0, 0.2, "ticker.FormatStrFormatter('>%d<')", |
60 |
| - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) |
| 76 | +ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", |
| 77 | + family=family, fontsize=fontsize, transform=ax.transAxes) |
61 | 78 |
|
62 | 79 | # Scalar formatter
|
63 |
| -ax = axs[4] |
| 80 | +ax = fig.add_subplot(n, 1, 5) |
| 81 | +setup(ax) |
64 | 82 | ax.xaxis.set_major_locator(ticker.AutoLocator())
|
65 | 83 | ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
|
66 | 84 | ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True))
|
67 |
| -ax.text(0.0, 0.2, "ticker.ScalarFormatter()", |
68 |
| - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) |
| 85 | +ax.text(0.0, 0.1, "ticker.ScalarFormatter()", |
| 86 | + family=family, fontsize=fontsize, transform=ax.transAxes) |
69 | 87 |
|
70 | 88 | # StrMethod formatter
|
71 |
| -ax = axs[5] |
| 89 | +ax = fig.add_subplot(n, 1, 6) |
| 90 | +setup(ax) |
72 | 91 | ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
|
73 | 92 | ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
|
74 | 93 | ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}"))
|
75 |
| -ax.text(0.0, 0.2, "ticker.StrMethodFormatter('{x}')", |
76 |
| - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) |
| 94 | +ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", |
| 95 | + family=family, fontsize=fontsize, transform=ax.transAxes) |
77 | 96 |
|
78 | 97 | # Percent formatter
|
79 |
| -ax = axs[6] |
| 98 | +ax = fig.add_subplot(n, 1, 7) |
| 99 | +setup(ax) |
80 | 100 | ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
|
81 | 101 | ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
|
82 | 102 | ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5))
|
83 |
| -ax.text(0.0, 0.2, "ticker.PercentFormatter(xmax=5)", |
84 |
| - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) |
| 103 | +ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", |
| 104 | + family=family, fontsize=fontsize, transform=ax.transAxes) |
| 105 | + |
| 106 | +# Push the top of the top axes outside the figure because we only show the |
| 107 | +# bottom spine. |
| 108 | +fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05) |
85 | 109 |
|
86 |
| -fig.savefig("../figures/tick-formatters.pdf") |
| 110 | +plt.savefig("../figures/tick-formatters.pdf", transparent=True) |
| 111 | +# plt.show() |
0 commit comments