Skip to content Skip to sidebar Skip to footer

Non-linear Second Axis In Matplotlib

I would like to know if there is a way to add second non-linear x-axis in Matplotlib if one doesnt have any analytical formula. Or to simplify if there is a way to create a distinc

Solution 1:

Since the question explicitely asks for arbitrary relation between the two axes (or refuses to clarify), here is a code that plots an arbitrary relation.

import matplotlib.pyplot as plt
import numpy as np

a, b = (2*np.random.rand(2)-1)*np.random.randint(1,500, size=2)
time = lambda T: a*T+b
Temp = lambda t: (t-b)/a

T = np.linspace(0, 100, 301)
y = T**2

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.25)

ax.set_xlabel("Temperature")
ax.plot(T,y)

ax2 = ax.secondary_xaxis(-0.2, functions=(time, Temp))
ax2.set_xlabel("Time")

plt.show()

The output may look like this, but may be different, because the relation is arbitrary and subject to change depending on the random numbers taken.

enter image description here

Post a Comment for "Non-linear Second Axis In Matplotlib"