如何在条形图matplotlib上对齐辅助轴

时间:2020-10-25 11:41:17

标签: matplotlib bar-chart

我在条形图中有两个轴。我用它来显示数据转换的影响(在这种情况下为z分数)。但是第二个轴(GDP_interval)未对齐。如何在条形上对齐它?

enter image description here

今天,我有以下代码:

    # Plot
    idx = numpy.arange(data.size)
    p1 = ax.bar(idx, data.values.flatten(), barWidth)

    # Axe
    # First axis
    ax.spines['bottom'].set_position(('outward', 40))
    ax.set_xlabel(data.index.names[0])
    ax.set_xticks(idx)
    ax.set_xticklabels(['({:.2f}, {:.2f}]'.format(i.left, i.right) for i in data.index.levels[0].values])

    # Second axis
    ax2 = ax.twiny()
    ax2.xaxis.set_ticks_position("bottom")
    ax2.xaxis.set_label_position("bottom")
    ax2.spines['bottom'].set_position(('outward', 0))
    ax2.set_xlabel(data.index.names[1])
    ax2.set_xticks([i + (barWidth / 2) for i in idx])
    ax2.set_xticklabels(['({:.0f}, {:.0f}]'.format(i.left, i.right) for i in data.index.levels[1].values])

0 个答案:

没有答案