【问题标题】:Multiple x-axis, which are nonlinear to each other多个 x 轴,彼此非线性
【发布时间】:2013-02-13 02:13:27
【问题描述】:

我正在尝试使用 matplotlib 绘制一个具有两个相互非线性的 x 轴的图形。我想得到的情节是这样的:

基本上,年龄取决于红移。它是非线性的,需要计算。我想将年龄和红移都作为 x 轴。我怎样才能做到?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    twiny() 函数可能就是你要找的。​​p>

    import matplotlib.pyplot as plt
    plt.loglog(range(100))
    ax1 = plt.gca()
    ax2 = ax1.twiny()
    ax2.set_xticks([100,80,50])
    ax2.set_xticklabels(['0','1','2'])
    ax1.set_xlabel('redshift')
    ax2.set_xlabel('age')
    plt.show()
    

    【讨论】:

    • 我做了,但由于我缺乏声誉而无法发布剧情。
    • 我没有明白这个函数的意义。看起来两个 x 轴仍然彼此成线性关系。
    • 我猜我假设非线性你的意思是对数。 xticks 将放置任意刻度,xticklabels 可以标记它们。希望对您有所帮助。
    • 非线性,我的意思是年龄可以通过积分从红移计算出来,比对数更复杂。
    【解决方案2】:

    我是这样做的:

    from mpl_toolkits.axes_grid.parasite_axes import SubplotHost 
    fig = plt.figure(1, figsize=(figwidth,figheight))
    ax = SubplotHost(fig, 1,1,1) 
    fig.add_subplot(ax)
    
    #plotting as usual
    
    ax2 = ax.twin() # ax2 is responsible for "top" axis and "right" axis
    ax2.axis["right"].toggle(ticklabels=False)
    ax2.xaxis.set_major_formatter(FuncFormatter(fmt_zToEta)) #set eta coord format
    
    #with a function for the Z to eta transform for plot labels
    def fmt_zToEta(x, pos=None):
        #...
        return transformed_label 
    

    我还记得从那个红移示例开始 ;-)

    我认为SubPlotHost 的事情是必要的,但我不是 100% 确定,因为我从我现有的(子)情节中撕下了它,而没有检查它是否运行良好。

    编辑:另见https://stackoverflow.com/a/10517481/599884

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多