我有一个图表,其中 x 轴是以 GeV 为单位的温度,但我还需要参考开尔文的温度,所以我想用 K 的温度放置一个寄生虫轴。尝试遵循这个答案如何在 matplotlib 中添加第二个 x 轴,这是代码示例。我在图表顶部得到了第二个轴,但它不是我需要的以 K 为单位的温度。
import numpy as np
import matplotlib.pyplot as plt
tt = np.logspace(-14,10,100)
yy = np.logspace(-10,-2,100)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
ax1.loglog(tt,yy)
ax1.set_xlabel('Temperature (GeV')
new_tick_locations = np.array([.2, .5, .9])
def tick_function(X):
V = X*1.16e13
return ["%.1f" % z for z in V]
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(ax1Xs))
ax2.set_xlabel('Temp (Kelvin)')
plt.show()
这是我运行代码时得到的。
对数图
我需要寄生虫轴与原始 x 轴成比例。当任何人看到图表时,都可以很容易地读取开尔文的温度。提前致谢。