如何使用pyplot(Python)使轴占据多个子图 [英] How to make an axes occupy multiple subplots with pyplot (Python)

查看:29
本文介绍了如何使用pyplot(Python)使轴占据多个子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个图中有三个图.该图应该有一个二乘二的子图布局,其中第一个图应占据前两个子图单元格(即整个第一行图单元格),其他图应位于单元格 3 和 4 中的第一个图下方.我知道matlab可以通过使用像这样的subplot命令来做到这一点

I would like to have three plots in single figure. The figure should have a subplot layout of two by two, where the first plot should occupy the first two subplot cells (i.e. the whole first row of plot cells) and the other plots should be positioned underneath the first one in cells 3 and 4. I know that matlab allows this by using the subplot command like so

subplot(2,2,[1,2]) % the plot will span subplots 1 and 2

在pyplot中是否也有可能让一个轴占据多个子图?pyplot.subplot的文档字符串没有讨论.

Is it also possible in pyplot to have a single axes occupy more than one subplot? The docstring of pyplot.subplot doesn't talk about it.

有人找到一个简单的解决方案吗?提前致谢

Anyone got an easy solution? Thanks in advance

推荐答案

您可以轻松地做到:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 7, 0.01)
    
plt.subplot(2, 1, 1)
plt.plot(x, np.sin(x))
    
plt.subplot(2, 2, 3)
plt.plot(x, np.cos(x))
    
plt.subplot(2, 2, 4)
plt.plot(x, np.sin(x)*np.cos(x))

即,第一个图其实是上半部的图(图中只分成了21 = 2个单元格),下面两个小图是在一个22=4单元格.subplot() 的第三个参数是网格内绘图的位置:例如,在第二个子图 (subplot(2, 2, 3)) 中,轴将到达 2*2 矩阵的第三部分,即左下角.

i.e., the first plot is really a plot in the upper half (the figure is only divided into 21 = 2 cells), and the following two smaller plots are done in a 22=4 cell grid. The third argument to subplot() is the positon of the plot inside the grid: for example in the second subplot (subplot(2, 2, 3)), the axes will go to the third section of the 2*2 matrix i.e, to the bottom-left corner.

这篇关于如何使用pyplot(Python)使轴占据多个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆