Matplotlib colorbar moves second x axis

前端 未结 2 899
孤城傲影
孤城傲影 2021-01-22 03:28

I\'m trying to add a second x axis to the top of a plot using twiny.

If I make a simple scatter plot with no colorbar, the top x axis is correctly aligned with the botto

2条回答
  •  广开言路
    2021-01-22 03:49

    This feels like kind of a hack. But by forcing the figure to draw. And then getting the ax1 position. you can set ax2 to the same and redraw the figure.

    ....
    cbar.ax.tick_params(labelsize=10)
    
    fig.canvas.draw()
    ax2.set_position(ax1.get_position())
    
    plt.show()
    

提交回复
热议问题