Matplotlib gridspec

编程入门 行业动态 更新时间:2024-10-10 23:21:55
Matplotlib gridspec - 在几个子图旁边放置另一个立方图(Matplotlib gridspec - placing another cubic plot right next to several 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()

如何使用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并更新每个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()

gridspec with two columns

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.

alternative gridspec

更多推荐

本文发布于:2023-07-30 14:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Matplotlib   gridspec

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!