|
4 | 4 | ====== |
5 | 5 |
|
6 | 6 | Illustrate the scale transformations applied to axes, e.g. log, symlog, logit. |
| 7 | +
|
| 8 | +The last two examples are examples of using the ``'arbitrary'`` scale by |
| 9 | +supplying forward and inverse functions for the scale transformation. |
7 | 10 | """ |
8 | 11 | import numpy as np |
9 | 12 | import matplotlib.pyplot as plt |
10 | | -from matplotlib.ticker import NullFormatter |
| 13 | +from matplotlib.ticker import NullFormatter, FixedLocator |
11 | 14 |
|
12 | 15 | # Fixing random state for reproducibility |
13 | 16 | np.random.seed(19680801) |
|
19 | 22 | x = np.arange(len(y)) |
20 | 23 |
|
21 | 24 | # plot with various axes scales |
22 | | -fig, axs = plt.subplots(2, 2, sharex=True) |
23 | | -fig.subplots_adjust(left=0.08, right=0.98, wspace=0.3) |
| 25 | +fig, axs = plt.subplots(3, 2, figsize=(6, 8), |
| 26 | + constrained_layout=True) |
24 | 27 |
|
25 | 28 | # linear |
26 | 29 | ax = axs[0, 0] |
|
54 | 57 | ax.yaxis.set_minor_formatter(NullFormatter()) |
55 | 58 |
|
56 | 59 |
|
| 60 | +# Arbitrary x**(1/2) |
| 61 | +def forward(x): |
| 62 | + return x**(1/2) |
| 63 | + |
| 64 | + |
| 65 | +def inverse(x): |
| 66 | + return x**2 |
| 67 | + |
| 68 | + |
| 69 | +ax = axs[2, 0] |
| 70 | +ax.plot(x, y) |
| 71 | +ax.set_yscale('arbitrary', functions=(forward, inverse)) |
| 72 | +ax.set_title('arbitrary: $x^{1/2}$') |
| 73 | +ax.grid(True) |
| 74 | +ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 1, 0.2)**2)) |
| 75 | +ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 1, 0.2))) |
| 76 | + |
| 77 | + |
| 78 | +# Arbitrary Mercator transform |
| 79 | +def forward(a): |
| 80 | + a = np.deg2rad(a) |
| 81 | + # mask values too close to +/- 90, which are ill-defined. |
| 82 | + masked = np.ma.masked_where((a <= -np.pi/2 + 0.02) | |
| 83 | + (a >= np.pi/2 - 0.02), a) |
| 84 | + if masked.mask.any(): |
| 85 | + return np.rad2deg( |
| 86 | + np.ma.log(np.abs(np.ma.tan(masked) + 1.0 / np.ma.cos(masked)))) |
| 87 | + else: |
| 88 | + return np.rad2deg(np.log(np.abs(np.tan(a) + 1.0 / np.cos(a)))) |
| 89 | + |
| 90 | + |
| 91 | +def inverse(a): |
| 92 | + a = np.deg2rad(a) |
| 93 | + return np.rad2deg(np.arctan(np.sinh(a))) |
| 94 | + |
| 95 | +ax = axs[2, 1] |
| 96 | + |
| 97 | +t = np.arange(-170.0, 170.0, 0.1) |
| 98 | +s = t / 2. |
| 99 | + |
| 100 | +ax.plot(t, s, '-', lw=2) |
| 101 | + |
| 102 | +ax.set_yscale('arbitrary', functions=(forward, inverse)) |
| 103 | +ax.set_title('arbitrary: Mercator') |
| 104 | +ax.grid(True) |
| 105 | +ax.set_xlim([-180, 180]) |
| 106 | +ax.yaxis.set_minor_formatter(NullFormatter()) |
| 107 | +ax.yaxis.set_major_locator(FixedLocator(np.arange(-90, 90, 30))) |
| 108 | + |
57 | 109 | plt.show() |
| 110 | + |
| 111 | +############################################################################# |
| 112 | +# |
| 113 | +# ------------ |
| 114 | +# |
| 115 | +# References |
| 116 | +# """""""""" |
| 117 | +# |
| 118 | +# The use of the following functions, methods, classes and modules is shown |
| 119 | +# in this example: |
| 120 | + |
| 121 | +import matplotlib |
| 122 | +matplotlib.axes.Axes.set_yscale |
| 123 | +matplotlib.axes.Axes.set_xscale |
| 124 | +matplotlib.axis.Axis.set_major_locator |
| 125 | +matplotlib.scale.LogitScale |
| 126 | +matplotlib.scale.LogScale |
| 127 | +matplotlib.scale.LinearScale |
| 128 | +matplotlib.scale.SymmetricalLogScale |
| 129 | +matplotlib.scale.ArbitraryScale |
0 commit comments