简体   繁体   English

我如何 plot 四个具有不同 colspans 的子图?

[英]How can I plot four subplots with different colspans?

I try to fit four images using matplotlib.pyplot like the following:我尝试使用matplotlib.pyplot来拟合四个图像,如下所示:

| plot1 | plot2|
|    plot3     |
|    plot4     |

Most examples I found cover three plots like these:我发现的大多数示例都包含如下三个图:

ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(212)

And this plots the three plots successfully (However, I don't get how it is done for ax3 ).这成功地绘制了三个图(但是,我不明白ax3是如何完成的)。 Now, I want to add the plot 4 to this arrangement.现在,我想将 plot 4 添加到此排列中。 Whatever I tried, I couldn't succeed.无论我尝试过什么,我都无法成功。

Could you please guide me how can I achieve it?你能指导我如何实现它吗?

You can use subplot2grid . 您可以使用subplot2grid It is really convenient. 真的很方便。

The docs say 医生说

Create a subplot in a grid. 在网格中创建子图。 The grid is specified by shape, at location of loc, spanning rowspan, colspan cells in each direction. 网格由形状指定,在loc的位置,跨行跨,在每个方向上的colspan单元格。 The index for loc is 0-based. loc的索引基于0。

First you define the size in terms of number of rows and columns (3,2) here. 首先,您在此处根据行数和列数(3,2)定义大小。 Then you define the starting (row, column) position for a particular subplot. 然后,为特定子图定义起始(行,列)位置。 Then you assign the number of rows/columns spanned by that particular subplot. 然后,分配该特定子图所跨越的行数/列数。 The keywords for row and column spans are rowspan and colspan respectively. 行跨度和列跨度的关键字分别是rowspancolspan

import matplotlib.pyplot as plt

ax1 = plt.subplot2grid((3, 2), (0, 0), colspan=1)
ax2 = plt.subplot2grid((3, 2), (0, 1), colspan=1)
ax3 = plt.subplot2grid((3, 2), (1, 0), colspan=2)
ax4 = plt.subplot2grid((3, 2), (2, 0), colspan=2)
plt.tight_layout()

在此处输入图片说明

The integer that you provide to subplot is actually 3 parts: 您提供给子图的整数实际上是3个部分:

  • first digit: number of rows 第一位:行数
  • second digit: number of columns 第二位数:列数
  • third digit: index 第三位数:索引

So for each call to subplots we specify how the plot area should be divided (using rows and cols) and then which area to put the plot in (using index), see images below. 因此,对于每个对子图的调用,我们指定应如何划分绘图区域(使用行和列),然后指定放置绘图的区域(使用索引),请参见下图。

ax1 = plt.subplot(321)  # 3 rows, 2 cols, index 1: col 1 on row 1
ax2 = plt.subplot(322)  # 3 rows, 2 cols, index 2: col 2 on row 1
ax3 = plt.subplot(312)  # 3 rows, 1 cols, index 2: col 1 on row 2
ax4 = plt.subplot(313)  # 3 rows, 1 cols, index 3: col 1 on row 3

From the docs : 文档

Either a 3-digit integer or three separate integers describing the position of the subplot. 3位整数或三个单独的整数描述子图的位置。 If the three integers are nrows, ncols, and index in order, the subplot will take the index position on a grid with nrows rows and ncols columns. 如果三个整数分别是nrows,ncols和index,则子图将在具有nrows行和ncols列的网格上取得索引位置。 index starts at 1 in the upper left corner and increases to the right. 索引从左上角的1​​开始并向右增加。

pos is a three digit integer, where the first digit is the number of rows, the second the number of columns, and the third the index of the subplot. pos是一个三位数的整数,其中第一位数是行数,第二位数是列数,第三位数是子图的索引。 ie fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). 即fig.add_subplot(235)与fig.add_subplot(2、3、5)相同。 Note that all integers must be less than 10 for this form to work. 请注意,所有整数必须小于10,此格式才能起作用。

321 312

In newer versions of matplotlib you can use subplot_mosaic like:在 matplotlib 的较新版本中,您可以像这样使用subplot_mosaic

fig, axes = plt.subplot_mosaic("AB;CC;DD")

It gives you four subplots in this structure:它为您提供了此结构中的四个子图:

                  AB          | A | B |
AB;CC;DD    ->    CC    ->    |   C   |
                  DD          |   D   |

To avoid overlapping labels, enable constrained_layout :为避免标签重叠,启用constrained_layout

fig, axes = plt.subplot_mosaic("AB;CC;DD", constrained_layout=True)

约束马赛克示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM