Matplotlib:获取并设置轴位置

时间:2014-04-17 15:59:20

标签: python matplotlib axes subplot

在matlab中,可以直接获取并设置图上现有轴的位置:

  pos = get(gca(), 'position')
  set(gca(), 'position', pos)

我如何在Matplotlib中执行此操作?

我出于两个相关的原因需要这个:

这些是我试图解决的具体问题:

  • 我有一列子图,其中一些有颜色条,有些没有颜色条,它们的宽度不同,即X轴不对齐。 colorbar从轴上窃取空间。这也发生在matlab中,我会使用上面的技巧通过将带有颜色条的轴的宽度复制到没有颜色条的轴来使所有轴同样宽。

  • 通过收缩轴在各个子图之间添加空格。 adjust_subplots()函数调整所有子图。

1 个答案:

答案 0 :(得分:42)

在Matplotlib中设置轴位置是类似的。您可以使用axes.

的get_position和set_position方法
import matplotlib.pyplot as plt

ax = plt.subplot(111)
pos1 = ax.get_position() # get the original position 
pos2 = [pos1.x0 + 0.3, pos1.y0 + 0.3,  pos1.width / 2.0, pos1.height / 2.0] 
ax.set_position(pos2) # set a new position

如果您还没有,可能还需要查看GridSpec

相关问题