【问题标题】:change position of top major x-ticks as function of bottom axis改变顶部主要 x 刻度的位置作为底部轴的函数
【发布时间】:2018-01-10 15:44:31
【问题描述】:

我想要类似于How to add a second x-axis in matplotlib 的东西,即有一个显示波长的顶部 x 轴和一个显示相应频率的底部轴。

复制链接示例给了我一个如下所示的情节:

这个情节是用:

#setting up the plot
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.gridspec as gridspec

fig = plt.figure()
fig.tight_layout()
ax = plt.subplot()
  

#Here it gets interesting!
def tick_function(X):
   c = 299792458
   V = c/X
   V = V*1e6
   V = np.round(V,0)
   V[2] = 3000
   V = V.astype(int)
   return(V)

ax = plt.subplot()
ax_top = ax.twiny()
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
ax_top.set_xscale("log", nonposx='clip')
ax.set_xlim([8e10,5e14])
ax.set_ylim([5e33,2e36])

axTicks = ax.get_xticks()   
ax_top_Ticks = axTicks
ax_top.set_xticks(ax_top_Ticks)
ax_top.set_xlim(ax.get_xlim())
ax_top.set_xbound(ax.get_xbound())
ax_top.set_xticklabels(tick_function(ax_top_Ticks))

现在,我不想在 底部 主要 x 轴的位置绘制 顶部 主要 x 刻度,而是让它们移动。 即,我希望在位置 1000、100、10、1 上放置顶部的主要 x 刻度,而次要刻度相应地移动。


这也是我想要的样子:

我找到了这个情节,这就是我想要的! http://inspirehep.net/record/877424/files/fig2.png 请注意,由于 lambda=c/f 和 ax & ax_top 是对数的,所以小刻度的间距必须反转为!

【问题讨论】:

  • 不是 100% 确定您的意思。您能否添加另一个示例屏幕截图来显示您希望该图的外观?
  • 您在脚本中使用的gs1 是什么?我需要它来重现您的结果。
  • @lkriener 我更新了,现在只有一个子图而不是令人困惑的网格规范......
  • @DavidG 我上传了一张我正在成像的照片...

标签: python matplotlib


【解决方案1】:

诀窍是选择您想要的波长并将它们转换为频率。然后将这些频率用作上部刻度的位置。

#setting up the plot
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure()
ax = plt.subplot()


def conversion_freq_lam(inp):
   c = 299792458
   outp = c/inp
   outp = outp.astype(int)
   return outp

#ax = plt.subplot(gs1[0])
ax = plt.subplot(111)
ax_top = ax.twiny()
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
ax_top.set_xscale("log", nonposx='clip')
ax.set_xlim([8e10,5e14])
ax.set_ylim([5e33,2e36])

goal_lambdas = np.array([100000, 10000, 1000, 100, 10, 1, 0.1, 0.01])
goal_freqs = conversion_freq_lam(goal_lambdas)
ax_top_Ticks = goal_freqs * 1e6  # magic factor 1e6 from your attempt. Units?
ax_top.set_xticks(ax_top_Ticks)
ax_top.set_xlim(ax.get_xlim())
ax_top.set_xbound(ax.get_xbound())
ax_top.set_xticklabels(goal_lambdas)

plt.savefig('test_2axes.png')

这会产生以下情节:

魔术数字1e6 用作我从您的问题中提取的比例因子。我认为它是由轴的单位引起的。

编辑:

要在上轴(例如 2、3、4、...、20、30、40、50...)处正确间隔小刻度,请添加以下代码块:

def find_minor_vals(goals):
    minors = []
    factors = np.arange(2, 10, 1)
    for val in goals:
        minors.extend(list(val * factors))
    print minors
    return np.array(minors)

goal_lambdas_minor = find_minor_vals(goal_lambdas)
goal_freqs_minor = conversion_freq_lam(goal_lambdas_minor) * 1e6
minor_locator = FixedLocator(goal_freqs_minor)
ax_top.xaxis.set_minor_locator(minor_locator)

结果如下图:

【讨论】:

  • 太好了,谢谢!现在这几乎是完美的,然而,仍然存在一个小问题:对于一个数量级 ax_top 有 7 个次要刻度,而 ax 有 8 个次要刻度。因此,因此 top_ax 的次要刻度不在 20,30,40 等处......
  • @Sebastiano1991 确实......我想我也可以使用与goal_lambdasgoal_freqs 相同的技巧来处理次要刻度。给我一点时间...
  • @Sebastiano1991 编辑帖子
猜你喜欢
  • 2021-02-06
  • 2018-02-04
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2020-01-15
  • 1970-01-01
相关资源
最近更新 更多