每一行共享?

Foo*_*Bar 23 python matplotlib

我有一个2比2的情节,我正在动态创建.在第一行我想绘制密度函数,在第二行CDF中.我想要

  • 每个列共享x
  • 每个行共享y

也就是说,垂直对齐的两个对象具有相同的x轴,并且两个水平对齐的图具有相同的y轴.

但是,sharexsharey强制它们对于所有子图都是相同的.如何修复这种轴共享?我知道我可以手动为每个轴分配一个共享伙伴,但这不适用于下面的通用结构:

fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True)
for i, lam in enumerate(lams):
    axesNow = [axs[i] for axs in axes]  # pick the ith column from axes
    for i, Param.p in enumerate(pp):
        axesNow[0].plot(somethingWithPDF)
        axesNow[1].plot(somethingWithCDF)

for ax in axes.flatten(): ax.legend()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

far*_*rth 54

pyplot.subplots文档描述了'col''row'该选项sharexshareykwargs.特别是,我想你想要:

fig, axes = plt.subplots(nrows=2, ncols=2, sharex='col', sharey='row')
Run Code Online (Sandbox Code Playgroud)


Jul*_*nck 0

像这样的东西怎么样,其中所有轴都是单独构建的:

x1 = np.arange(5)
y1 = np.arange(3, 8)
ax1 = plt.subplot(223)
ax1.plot(x1, y1)
ax1.set_title("ax1")

x2 = np.arange(5, 10)
y2 = np.arange(3, 8)
ax2 = plt.subplot(224, sharey=ax1)
ax2.plot(x2, y2)
ax2.set_title("ax2")
#plt.setp(ax2.get_yticklabels(), visible=False) # Use this to hide axes labels


x3 = x1
y3 = np.arange(13, 8, -1)
ax3 = plt.subplot(221, sharex=ax1)
ax3.plot(x3, y3)
ax3.set_title("ax3")
#plt.setp(ax3.get_xticklabels(), visible=False)


x4 = x2
y4 = y3
ax4 = plt.subplot(222, sharex=ax2, sharey=ax3)
ax4.plot(x4, y4)
ax4.set_title("ax4")
#plt.setp(ax4.get_xticklabels(), visible=False)
#plt.setp(ax4.get_yticklabels(), visible=False)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述