Python,Matplotlib自定义轴共享Y轴

时间:2016-02-10 15:14:11

标签: python matplotlib axis axes

我有简单的代码来创建一个包含7个轴/自定义子图的图形(我的理解是子图是相等大小和等间距的,在我的特定情况下,我需要一个比其余的大一些)。

fig = plt.figure(figsize = (16,12))
# row 1
ax1 = plt.axes([0.1,0.7,0.2,0.2])
ax2 = plt.axes([0.4,0.7,0.2,0.2])
ax3 = plt.axes([0.7,0.7,0.2,0.2])
# big row 2
ax4 = plt.axes([0.1, 0.4, 0.5, 0.2])
#row 3
ax5 = plt.axes([0.1,0.1,0.2,0.2])
ax6 = plt.axes([0.4,0.1,0.2,0.2])
ax7 = plt.axes([0.7,0.1,0.2,0.2])

我的问题是,如何让所有这些轴共享相同的y轴。我在谷歌/堆栈上找到的所有内容都是用于子图,例如:

ax = plt.subplot(blah, sharey=True)

但是为轴创建调用相同的东西不起作用:

ax = plt.axes([blah], sharey=True) # throws error

无论如何要实现这个目标吗?我与之合作的是:

Desired fig layout

1 个答案:

答案 0 :(得分:2)

使用matplotlib.gridspec.GridSpec

非常简单

<table class="table table-striped table-hover"> <thead> <tr> <th></th> <th></th> <th></th> </tr> </thead> <tbody id="availableApps"> </tbody> </table> 创建一个3x3网格以在

上放置子图

对于您的上行和下行,我们只需要在该3x3网格上索引一个单元格(例如gs=GridSpec(3,3)位于左上角)。

对于中间行,您需要跨越两列,因此我们使用gs[0,0]

gs[1,0:2]

enter image description here