【问题标题】:Having python plot two different x-axes with different scales让python绘制两个不同比例的不同x轴
【发布时间】:2020-03-31 02:06:11
【问题描述】:

到目前为止,这是我的代码:

QE_ellip_fixed = [-1.04e-3,-1.04e-2,-0.1,-0.76,-2.34,-2.54]
QL_ellip_fixed = [1.77e-4,9.89e-4,-6e-2,-2.9,-4.45,-2.74]
QP_ellip_fixed = [1.26e-3,1.45e-2,0.14,0.98,2.6,2.5]
QE_ellip_varied = [-1.73e-4,-1.73e-3,-1.71e-2,-0.15,-0.86,-3.16]
QL_ellip_varied = [7.57e-5,7.53e-4,5.4e-3,-0.13,-4.15,-7.3]
QP_ellip_varied = [1.41e-3,1.77e-3,2.34e-2,0.22,1.33,3.14]
RHScalls_ellip = [764021,76388,7625,750,63,3]
RHScalls_circ = [629171,62864,6234,577,41,5]
QE_circ_fixed= [-1.26e-4,-1.26e-3,-1.24e-2,-0.11,-0.57,-2.98]
QL_circ_fixed = [-1.32e-4,5.89e-4,1.5e-3,-0.51,0.4,-9.57]
QP_circ_fixed = [1.45e-2,9.25e-3,4.62e-2,0.58,3.5,8.54]
QE_circ_varied = [-1.26e-4,-1.25e-3,-1.24e-2,-0.11,-0.56,-2.13]
QL_circ_varied = [-1.33e-4,5.88e-4,1.69e-3,-0.45,-0.64,-6.58]
QP_circ_varied = [1.45e-2,9.32e-3,5.2e-2,0.55,3.11,13.05]
alp = [1e-5,1e-4,1e-3,1e-2,1e-1,1]

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(alp,np.abs(QE_ellip_varied),label='$|Q_E|$')
ax1.plot(alp,np.abs(QL_ellip_varied),label='$|Q_L|$')
ax1.plot(alp,np.abs(QP_ellip_varied),label='$|Q_P|$')
ax2 = ax1.twiny()
ax2.set_xticks([1e-5,1e-4,1e-3,1e-2,1e-1,1])
ax2.set_xticklabels(RHScalls_ellip)
ax1.set_xscale('log')
plt.yscale('log')
ax1.grid()
ax1.set_xlabel('alpha')
ax1.set_ylabel('Score (unitless)')
ax1.legend()


plt.show()

这是输出的图像:

我想让顶轴上的值具有与已经施加的网格线一致的刻度标签,因为它们实际上对应于那些网格线,但我似乎无法让顶轴不以这种烦人的对数方式运行.我只为轴 1 指定了对数刻度,但它似乎也适用于轴 2...

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这是因为轴共享 y 轴,但不共享 x 轴。每个的 xlimits 都不同..以下对我有用:

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twiny()
    ax1.set_xscale('log')
    ax2.set_xscale('log') #make sure both log
    plt.yscale('log')
    ax1.plot(alp,np.abs(QE_ellip_varied),label='$|Q_E|$')
    ax1.plot(alp,np.abs(QL_ellip_varied),label='$|Q_L|$')
    ax1.plot(alp,np.abs(QP_ellip_varied),label='$|Q_P|$')
    ax2.set_xlim(ax1.get_xlim()) #make sure same limits
    ax2.set_xticks([1e-5,1e-4,1e-3,1e-2,1e-1,1])
    ax2.set_xticklabels(RHScalls_ellip)
    ax1.grid()
    ax1.set_xlabel('alpha')
    ax1.set_ylabel('Score (unitless)')
    ax1.legend()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-08
      • 2021-11-26
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多