我正试图找出如何在三个子图的右侧放置另一个立方体形状的图
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs1 = gridspec.GridSpec(3,2) ax1 = plt.subplot(gs1[0, :]) ax2 = plt.subplot(gs1[1, :],sharex=ax1) ax3 = plt.subplot(gs1[2, :],sharex=ax1) plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax2.get_xticklabels(), visible=False) plt.show()如何使用gridspec放置另一个跨越那三行的gridspec ?
O am trying to work out how to place another plot, of a cubic shape, on the right of three subplots
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs1 = gridspec.GridSpec(3,2) ax1 = plt.subplot(gs1[0, :]) ax2 = plt.subplot(gs1[1, :],sharex=ax1) ax3 = plt.subplot(gs1[2, :],sharex=ax1) plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax2.get_xticklabels(), visible=False) plt.show()How can I place another plot that spans the three rows next to those one using gridspec?
最满意答案
您已经通过将它细分为两列来为gridspec定义了正确的拆分。 指定左轴使用第一列(请参阅下面的更改),应该是“立方”(纵横比1)的轴使用gridspec的右列。
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs1 = gridspec.GridSpec(3,2) ax1 = plt.subplot(gs1[0, 0]) ax2 = plt.subplot(gs1[1, 0],sharex=ax1) ax3 = plt.subplot(gs1[2, 0],sharex=ax1) ax4 = plt.subplot(gs1[:, 1]) # NEW ax4.set_aspect('equal', adjustable='box') # NEW plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax2.get_xticklabels(), visible=False) plt.show()
或者,您可以定义第二个gridspec并更新每个gridspec的(相对)位置,如下所示:
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs1 = gridspec.GridSpec(3,2) gs1.update(left=0.05, right=0.48, wspace=0.05) ax1 = plt.subplot(gs1[0, :]) ax2 = plt.subplot(gs1[1, :],sharex=ax1) ax3 = plt.subplot(gs1[2, :],sharex=ax1) gs2 = gridspec.GridSpec(1, 1) gs2.update(left=0.55, right=0.98, hspace=0.05) ax4 = plt.subplot(gs2[0,0]) #ax4.set_aspect('equal', adjustable='box') plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax2.get_xticklabels(), visible=False) plt.show()我评论了代码在ax4获得相等的宽高比,突出显示它默认填充整个可用空间。
You've already defined a proper splitting for the gridspec by subdividing it into two columns. Specify that the left axes use the first column (see changes below) and the axes which is supposed to be "cubic" (aspect ratio 1) uses the right column of your gridspec.
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs1 = gridspec.GridSpec(3,2) ax1 = plt.subplot(gs1[0, 0]) ax2 = plt.subplot(gs1[1, 0],sharex=ax1) ax3 = plt.subplot(gs1[2, 0],sharex=ax1) ax4 = plt.subplot(gs1[:, 1]) # NEW ax4.set_aspect('equal', adjustable='box') # NEW plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax2.get_xticklabels(), visible=False) plt.show()Alternatively, you could define a second gridspec and update the (relative) positioning of each gridspec, like so:
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs1 = gridspec.GridSpec(3,2) gs1.update(left=0.05, right=0.48, wspace=0.05) ax1 = plt.subplot(gs1[0, :]) ax2 = plt.subplot(gs1[1, :],sharex=ax1) ax3 = plt.subplot(gs1[2, :],sharex=ax1) gs2 = gridspec.GridSpec(1, 1) gs2.update(left=0.55, right=0.98, hspace=0.05) ax4 = plt.subplot(gs2[0,0]) #ax4.set_aspect('equal', adjustable='box') plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax2.get_xticklabels(), visible=False) plt.show()I commented the code to get an equal aspect ratio in ax4, to highlight that by default, it fills the entire available space.
更多推荐
发布评论