【问题标题】:How can I make a figure that has two x-axis? [closed]如何制作具有两个 x 轴的图形? [关闭]
【发布时间】:2016-09-11 17:44:26
【问题描述】:

我想用python制作一个这样的图。我是用excel画的。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您将不得不构建类似两个图的东西,并使用其中一个的 x 轴作为辅助轴。您的绘图中还有其他功能,例如为刻度及其位置设置标签。以下示例对这些元素中的每一个进行了一些处理,因此您应该能够适应您的问题:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twiny()
    
    # Add some extra space for the second axis at the bottom
    fig.subplots_adjust(bottom=0.2)
    ax1.set_xticks([1,2,4,5,7,8])
    ax1.set_xlim(0,9)
    ax1.set_xticklabels(('2015','2016','2015','2016','2015','2016'))
    ax2.spines["bottom"].set_position(("axes", -0.15))
    ax2.xaxis.set_ticks_position("bottom")
    ax2.spines["bottom"].set_visible(True)
    ax2.set_xticks([1.5,4.5,7.5])
    ax2.set_xticklabels(('1','2','3'))
    ax2.set_xlim(0,9)
    
    b1 = np.random.randint(0,100,6)
    b2 = np.random.randint(0,100,6)
    b3 = np.random.randint(0,100,6)
    plt.bar(np.array([1,2,4,5,7,8])-0.4,b1,color='blue')
    plt.bar(np.array([1,2,4,5,7,8])-0.4,b2,color='orange',bottom=b1)
    plt.bar(np.array([1,2,4,5,7,8])-0.4,b3,color='yellow',bottom=b1+b2)
    
    plt.show()
    

    ,结果是这样的:

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多