Matplotlib: one line, plotted against two related x axes in different units?

Just for completeness: There exists 'secondary axes' (matplotlib docs):

ax.secondary_xaxis('top', functions=(to_new_axis, from_new_axis))

the two functions

to_new_axis(), from_new_axis()

need to give proper scaling between say, your current units, and different units.

Code from the documentation:

fig, ax = plt.subplots(constrained_layout=True)
x = np.arange(0, 360, 1)
y = np.sin(2 * x * np.pi / 180)
ax.plot(x, y)
ax.set_xlabel('angle [degrees]')
ax.set_ylabel('signal')
ax.set_title('Sine wave')

def deg2rad(x):
    return x * np.pi / 180

def rad2deg(x):
    return x * 180 / np.pi

secax = ax.secondary_xaxis('top', functions=(deg2rad, rad2deg))
secax.set_xlabel('angle [rad]')
plt.show()

Here, they show the sinus function, in units of degrees and radians. In case you do not have well defined functions to switch between scales, you can use numpy interpolation:

def forward(x):
    return np.interp(x, xold, xnew)

def inverse(x):
    return np.interp(x, xnew, xold)

For different x-scales use twiny() (think of this as "shared y-axes"). An example slightly adapted from the matplotlib documentation:

import numpy as np
import matplotlib.pyplot as plt

# plot f(x)=x for two different x ranges
x1 = np.linspace(0, 1, 50)
x2 = np.linspace(0, 2, 50)
fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x1, x1,'b--')

ax2 = ax1.twiny()
ax2.plot(x2, x2, 'go')

plt.show()

If you just wanted a second axis plot the second data set as invisible.

ax2.plot(x2, x2, alpha=0)