Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd1c6cc

Browse files
committed
Update secondary_axis tutorial when the second axis is interpolated from the first. In this case the interpolation must be defined outside the bounds of the data that is plotted. Moreover the tutorial in question has been simplified.
1 parent 773096e commit cd1c6cc

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

galleries/examples/subplots_axes_and_figures/secondary_axis.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import numpy as np
1818

1919
import matplotlib.dates as mdates
20-
from matplotlib.ticker import AutoMinorLocator
2120

2221
fig, ax = plt.subplots(layout='constrained')
2322
x = np.arange(0, 360, 1)
@@ -96,48 +95,50 @@ def one_over(x):
9695
plt.show()
9796

9897
# %%
99-
# Sometime we want to relate the axes in a transform that is ad-hoc from
100-
# the data, and is derived empirically. In that case we can set the
101-
# forward and inverse transforms functions to be linear interpolations from the
102-
# one data set to the other.
98+
# Sometime we want to relate the axes in a transform that is ad-hoc from the data, and
99+
# is derived empirically. Or, one axis could be a complicated nonlinear function of the
100+
# other. In these cases we can set the forward and inverse transform functions to be
101+
# linear interpolations from the one set of independent variables to the other.
103102
#
104103
# .. note::
105104
#
106105
# In order to properly handle the data margins, the mapping functions
107106
# (``forward`` and ``inverse`` in this example) need to be defined beyond the
108-
# nominal plot limits.
109-
#
110-
# In the specific case of the numpy linear interpolation, `numpy.interp`,
111-
# this condition can be arbitrarily enforced by providing optional keyword
112-
# arguments *left*, *right* such that values outside the data range are
113-
# mapped well outside the plot limits.
107+
# nominal plot limits. This condition can be enforced by extending the
108+
# interpolation beyond the plotted values, both to the left and the right,
109+
# see ``x1n`` and ``x2n`` below.
114110

115111
fig, ax = plt.subplots(layout='constrained')
116-
xdata = np.arange(1, 11, 0.4)
117-
ydata = np.random.randn(len(xdata))
118-
ax.plot(xdata, ydata, label='Plotted data')
119-
120-
xold = np.arange(0, 11, 0.2)
121-
# fake data set relating x coordinate to another data-derived coordinate.
122-
# xnew must be monotonic, so we sort...
123-
xnew = np.sort(10 * np.exp(-xold / 4) + np.random.randn(len(xold)) / 3)
124-
125-
ax.plot(xold[3:], xnew[3:], label='Transform data')
126-
ax.set_xlabel('X [m]')
112+
x1_vals = np.arange(2, 11, 0.4)
113+
# second independent variable is a nonlinear function of the other.
114+
# this simple example can be more easily handled without interpolation, and is done
115+
# this way only for pedagogical purposes.
116+
x2_vals = x1_vals ** 2
117+
ydata = 50.0 + 20 * np.random.randn(len(x1_vals))
118+
ax.plot(x1_vals, ydata, label='Plotted data')
119+
ax.plot(x1_vals, x2_vals, label=r'$x_2(x_1)$')
120+
ax.set_xlabel(r'$x_1$')
127121
ax.legend()
128122

123+
# need to define mapped values outside of plotted range to ensure the secondary
124+
# axis is plotted correctly
125+
x1n = np.concatenate(([0.0], x1_vals, [20]))
126+
x2n = np.concatenate(([0.0], x2_vals, [20**2]))
127+
129128

130129
def forward(x):
131-
return np.interp(x, xold, xnew)
130+
return np.interp(x, x1n, x2n)
132131

133132

134133
def inverse(x):
135-
return np.interp(x, xnew, xold)
136-
134+
return np.interp(x, x2n, x1n)
137135

136+
# use axvline to prove that the derived secondary axis is correctly plotted
137+
ax.axvline(np.sqrt(40), color="grey", ls="--")
138+
ax.axvline(10, color="grey", ls="--")
138139
secax = ax.secondary_xaxis('top', functions=(forward, inverse))
139-
secax.xaxis.set_minor_locator(AutoMinorLocator())
140-
secax.set_xlabel('$X_{other}$')
140+
secax.set_xticks([10, 20, 40, 60, 80, 100])
141+
secax.set_xlabel(r'$x_2$')
141142

142143
plt.show()
143144

0 commit comments

Comments
 (0)