Matplotlib 中的非线性第二轴
Non-linear Second Axis in Matplotlib
我想知道如果没有任何分析公式,是否有办法在 Matplotlib 中添加第二个非线性 x 轴。或者,如果有办法为原始 x 轴中的每个数字创建不同的标签,则可以简化。下图解释了我在寻找什么。不幸的是,类似的 question 之前曾被问过但没有得到回答。
由于问题明确要求两个轴之间的任意关系(或拒绝澄清),这里是绘制任意关系的代码。
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()
输出可能看起来像这样,但可能会有所不同,因为关系是任意的,并且会根据所采用的随机数而发生变化。
我想知道如果没有任何分析公式,是否有办法在 Matplotlib 中添加第二个非线性 x 轴。或者,如果有办法为原始 x 轴中的每个数字创建不同的标签,则可以简化。下图解释了我在寻找什么。不幸的是,类似的 question 之前曾被问过但没有得到回答。
由于问题明确要求两个轴之间的任意关系(或拒绝澄清),这里是绘制任意关系的代码。
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()
输出可能看起来像这样,但可能会有所不同,因为关系是任意的,并且会根据所采用的随机数而发生变化。