I have datasets of X and Y, which I plot. For each value of X I have another dataset. I want to plot 2 x-axes in matplotlib. The problem is that the datasets for x are not dependent on each other.
I tried this StackOverflow solution. When I have no x-lims, it works well.
import matplotlib.pyplot as pltimport numpy as npfig, ax = plt.subplots(constrained_layout=True)x_upper = np.array([2.379618e-29,5.895669e-29,1.018725e-28,1.413803e-28,1.780809e-28,2.080620e-28,2.287013e-28,2.603605e-28,2.810124e-28])x_lower = np.array([20,30,40,50,60,70,80,100,120])y = np.array([-1.333526e-16,-8.841676e-16,-2.803599e-15,-5.300285e-15,-7.723194e-15,-9.906261e-15,-1.124626e-14,-1.238456e-14,-1.193596e-14])ax.plot(x_lower, y)xold = x_lowerxnew = x_upperdef forward(x): return np.interp(x, xold, xnew)def inverse(x): return np.interp(x, xnew, xold)secax = ax.secondary_xaxis('top', functions=(forward,inverse))plt.autoscale(enable=True, axis='x', tight=True)
However, in the upper x-axis, always the values 0
and 3e-28
are shown. How to get rid of them? The next problem is when I change the lower axis limit:
ax.set_xlim(15, 125)
The values are still there, but now the lower x-axis and the upper x-axis don't match their values. Could you please help me fix it?