1

我有一个图表,其中 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 轴成比例。当任何人看到图表时,都可以很容易地读取开尔文的温度。提前致谢。

4

2 回答 2

1

问题似乎如下:当您使用时ax2.set_xlim(ax1.get_xlim()),您基本上将上 x 轴的限制设置为与下 x 轴的限制相同。现在如果你这样做

print(ax1.get_xlim()) 
print(ax2.get_xlim()) 

你得到两个轴相同的值

(6.309573444801943e-16, 158489319246.11108) 
(6.309573444801943e-16, 158489319246.11108) 

但是您的下 x 轴具有对数刻度。当您使用 分配限制时ax2.set_xlim(), 的限制ax2是相同的,但比例仍然是线性的。这就是为什么当您将刻度设置为 时[.2, .5, .9],这些值显示为上 x 轴最左侧的刻度,如图所示。

解决方案是将上部 x 轴也设置为对数刻度。这是必需的,因为您new_tick_locations对应于较低 x 轴上的实际值。您只想重命名这些值以在开尔文中显示刻度标签。从您的变量名称中可以清楚地看出new_tick_locations与新的刻度位置相对应。我使用一些修改后的值new_tick_locations来突出问题。

我正在使用科学格式'%.0e',因为 1 GeV = 1.16e13 K,所以 0.5 GeV 将是一个非常大的值,有很多零。

下面是一个示例答案:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

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([0.000002, 0.05, 9000])

def tick_function(X):
    V = X*1.16e13
    return ["%.1f" % z for z in V]

ax2.set_xscale('log') # Setting the logarithmic scale
ax2.set_xlim(ax1.get_xlim())

ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.0e'))

ax2.set_xlabel('Temp (Kelvin)')
plt.show()

在此处输入图像描述

于 2019-02-12T00:20:08.600 回答
1

通用解决方案可能如下所示。由于您有一个非线性刻度,因此想法是以开尔文为单位找到漂亮刻度的位置,转换为 GeV,以 GeV 为单位设置位置,但以开尔文为单位标记它们。这听起来很复杂,但好处是您不需要自己查找刻度,只需依靠 matplotlib 来查找它们。然而,这需要的是两个尺度之间的函数依赖关系,即 GeV 和 Kelvin 之间的转换及其倒数。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

tt = np.logspace(-14,10,100)
yy = np.logspace(-10,-2,100)

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()

plt.setp([ax1,ax2], xscale="log", yscale="log")
ax1.get_shared_x_axes().join(ax1, ax2)

ax1.plot(tt,yy)

ax1.set_xlabel('Temperature (GeV)')
ax2.set_xlabel('Temp (Kelvin)')

fig.canvas.draw()

# 1 GeV == 1.16 × 10^13 Kelvin
Kelvin2GeV = lambda k:  k / 1.16e13
GeV2Kelvin = lambda gev: gev * 1.16e13

loc = mticker.LogLocator()
locs = loc.tick_values(*GeV2Kelvin(np.array(ax1.get_xlim())))

ax2.set_xticks(Kelvin2GeV(locs))
ax2.set_xlim(ax1.get_xlim())

f = mticker.ScalarFormatter(useOffset=False, useMathText=True)
g = lambda x,pos : "${}$".format(f._formatSciNotation('%1.10e' % GeV2Kelvin(x)))
fmt = mticker.FuncFormatter(g)
ax2.xaxis.set_major_formatter(mticker.FuncFormatter(fmt))

plt.show()

在此处输入图像描述

于 2019-02-12T02:45:19.303 回答