|
| 1 | +""" |
| 2 | +============== |
| 3 | +Secondary Axis |
| 4 | +============== |
| 5 | +
|
| 6 | +Sometimes we want as secondary axis on a plot, for instance to convert |
| 7 | +radians to degrees on the same plot. We can do this by making a child |
| 8 | +axes with only one axis visible via `.Axes.axes.secondary_xaxis` and |
| 9 | +`.Axes.axes.secondary_yaxis`. |
| 10 | +
|
| 11 | +If we want to label the top of the axes: |
| 12 | +""" |
| 13 | + |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | +import datetime |
| 17 | +import matplotlib.dates as mdates |
| 18 | +from matplotlib.transforms import Transform |
| 19 | +from matplotlib.ticker import ( |
| 20 | + AutoLocator, AutoMinorLocator) |
| 21 | + |
| 22 | +fig, ax = plt.subplots(constrained_layout=True) |
| 23 | +x = np.arange(0, 360, 1) |
| 24 | +y = np.sin(2 * x * np.pi / 180) |
| 25 | +ax.plot(x, y) |
| 26 | +ax.set_xlabel('angle [degrees]') |
| 27 | +ax.set_ylabel('signal') |
| 28 | +ax.set_title('Sine wave') |
| 29 | +secax = ax.secondary_xaxis('top') |
| 30 | +plt.show() |
| 31 | + |
| 32 | +########################################################################### |
| 33 | +# However, its often useful to label the secondary axis with something |
| 34 | +# other than the labels in the main axis. In that case we need to provide |
| 35 | +# both a forward and an inverse conversion function in a tuple |
| 36 | +# to the ``functions`` kwarg: |
| 37 | + |
| 38 | +fig, ax = plt.subplots(constrained_layout=True) |
| 39 | +x = np.arange(0, 360, 1) |
| 40 | +y = np.sin(2 * x * np.pi / 180) |
| 41 | +ax.plot(x, y) |
| 42 | +ax.set_xlabel('angle [degrees]') |
| 43 | +ax.set_ylabel('signal') |
| 44 | +ax.set_title('Sine wave') |
| 45 | + |
| 46 | + |
| 47 | +def deg2rad(x): |
| 48 | + return x * np.pi / 180 |
| 49 | + |
| 50 | + |
| 51 | +def rad2deg(x): |
| 52 | + return x * 180 / np.pi |
| 53 | + |
| 54 | +secax = ax.secondary_xaxis('top', functions=(deg2rad, rad2deg)) |
| 55 | +secax.set_xlabel('angle [rad]') |
| 56 | +plt.show() |
| 57 | + |
| 58 | +########################################################################### |
| 59 | +# Here is the case of converting from wavenumber to wavelength in a |
| 60 | +# log-log scale. |
| 61 | +# |
| 62 | +# .. note :: |
| 63 | +# |
| 64 | +# In this case, the xscale of the parent is logarithmic, so the child is |
| 65 | +# made logarithmic as well. |
| 66 | + |
| 67 | +fig, ax = plt.subplots(constrained_layout=True) |
| 68 | +x = np.arange(0.02, 1, 0.02) |
| 69 | +np.random.seed(19680801) |
| 70 | +y = np.random.randn(len(x)) ** 2 |
| 71 | +ax.loglog(x, y) |
| 72 | +ax.set_xlabel('f [Hz]') |
| 73 | +ax.set_ylabel('PSD') |
| 74 | +ax.set_title('Random spectrum') |
| 75 | + |
| 76 | + |
| 77 | +def forward(x): |
| 78 | + return 1 / x |
| 79 | + |
| 80 | + |
| 81 | +def inverse(x): |
| 82 | + return 1 / x |
| 83 | + |
| 84 | +secax = ax.secondary_xaxis('top', functions=(forward, inverse)) |
| 85 | +secax.set_xlabel('period [s]') |
| 86 | +plt.show() |
| 87 | + |
| 88 | +########################################################################### |
| 89 | +# Sometime we want to relate the axes in a transform that is ad-hoc from |
| 90 | +# the data, and is derived empirically. In that case we can set the |
| 91 | +# forward and inverse transforms functions to be linear interpolations from the |
| 92 | +# one data set to the other. |
| 93 | + |
| 94 | +fig, ax = plt.subplots(constrained_layout=True) |
| 95 | +xdata = np.arange(1, 11, 0.4) |
| 96 | +ydata = np.random.randn(len(xdata)) |
| 97 | +ax.plot(xdata, ydata, label='Plotted data') |
| 98 | + |
| 99 | +xold = np.arange(0, 11, 0.2) |
| 100 | +# fake data set relating x co-ordinate to another data-derived co-ordinate. |
| 101 | +xnew = np.sort(10 * np.exp(-xold / 4) + np.random.randn(len(xold)) / 3) |
| 102 | + |
| 103 | +ax.plot(xold[3:], xnew[3:], label='Transform data') |
| 104 | +ax.set_xlabel('X [m]') |
| 105 | +ax.legend() |
| 106 | + |
| 107 | + |
| 108 | +def forward(x): |
| 109 | + return np.interp(x, xold, xnew) |
| 110 | + |
| 111 | + |
| 112 | +def inverse(x): |
| 113 | + return np.interp(x, xnew, xold) |
| 114 | + |
| 115 | +secax = ax.secondary_xaxis('top', functions=(forward, inverse)) |
| 116 | +secax.xaxis.set_minor_locator(AutoMinorLocator()) |
| 117 | +secax.set_xlabel('$X_{other}$') |
| 118 | + |
| 119 | +plt.show() |
| 120 | + |
| 121 | +########################################################################### |
| 122 | +# A final example translates np.datetime64 to yearday on the x axis and |
| 123 | +# from Celsius to Farenheit on the y axis: |
| 124 | + |
| 125 | + |
| 126 | +dates = [datetime.datetime(2018, 1, 1) + datetime.timedelta(hours=k * 6) |
| 127 | + for k in range(240)] |
| 128 | +temperature = np.random.randn(len(dates)) |
| 129 | +fig, ax = plt.subplots(constrained_layout=True) |
| 130 | + |
| 131 | +ax.plot(dates, temperature) |
| 132 | +ax.set_ylabel(r'$T\ [^oC]$') |
| 133 | +plt.xticks(rotation=70) |
| 134 | + |
| 135 | + |
| 136 | +def date2yday(x): |
| 137 | + """ |
| 138 | + x is in matplotlib datenums, so they are floats. |
| 139 | + """ |
| 140 | + y = x - mdates.date2num(datetime.datetime(2018, 1, 1)) |
| 141 | + return y |
| 142 | + |
| 143 | + |
| 144 | +def yday2date(x): |
| 145 | + """ |
| 146 | + return a matplotlib datenum (x is days since start of year) |
| 147 | + """ |
| 148 | + y = x + mdates.date2num(datetime.datetime(2018, 1, 1)) |
| 149 | + return y |
| 150 | + |
| 151 | +secaxx = ax.secondary_xaxis('top', functions=(date2yday, yday2date)) |
| 152 | +secaxx.set_xlabel('yday [2018]') |
| 153 | + |
| 154 | + |
| 155 | +def CtoF(x): |
| 156 | + return x * 1.8 + 32 |
| 157 | + |
| 158 | + |
| 159 | +def FtoC(x): |
| 160 | + return (x - 32) / 1.8 |
| 161 | + |
| 162 | +secaxy = ax.secondary_yaxis('right', functions=(CtoF, FtoC)) |
| 163 | +secaxy.set_ylabel(r'$T\ [^oF]$') |
| 164 | + |
| 165 | +plt.show() |
| 166 | + |
| 167 | +############################################################################# |
| 168 | +# |
| 169 | +# ------------ |
| 170 | +# |
| 171 | +# References |
| 172 | +# """""""""" |
| 173 | +# |
| 174 | +# The use of the following functions and methods is shown in this example: |
| 175 | + |
| 176 | +import matplotlib |
| 177 | + |
| 178 | +matplotlib.axes.Axes.secondary_xaxis |
| 179 | +matplotlib.axes.Axes.secondary_yaxis |
0 commit comments