Matplotlib中的非线性第二轴

问题描述 投票:4回答:1

我想知道如果没有任何解析公式,是否可以在Matplotlib中添加第二个非线性x轴。或者简化一下,是否可以为原始x轴上的每个数字创建不同的标签。下图说明了我要寻找的东西。不幸的是,之前曾有人问过类似的question,但没有得到答复。

enter image description here

python matplotlib plot axis-labels
1个回答
2
投票

由于该问题明确要求两个轴之间的任意关系(或拒绝阐明),因此这里的代码绘制了任意关系。

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()

输出可能看起来像这样,但是可能有所不同,因为该关系是任意的,并且可能根据所取的随机数而改变。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.