如何绘制具有不同 colspan 的四个子图? [英] How can I plot four subplots with different colspans?
问题描述
我尝试使用 matplotlib.pyplot
拟合四个图像,如下所示:
<代码>|情节1 |情节2||情节3 ||情节4 |
我发现的大多数例子都涵盖了这样的三个图:
ax1 = plt.subplot(221)ax2 = plt.subplot(222)ax3 = plt.subplot(212)
这成功地绘制了三个图(但是,我不明白 ax3
是如何完成的).现在,我想将情节 4 添加到此安排中.无论我尝试什么,我都无法成功.
能否请您指导我如何实现它?
您可以使用
I try to fit four images using matplotlib.pyplot
like the following:
| 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
). Now, I want to add the plot 4 to this arrangement. Whatever I tried, I couldn't succeed.
Could you please guide me how can I achieve it?
You can use 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. The index for loc is 0-based.
First you define the size in terms of number of rows and columns (3,2)
here. 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.
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()
这篇关于如何绘制具有不同 colspan 的四个子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!