在Matplotlib中定位5个子图 [英] Position 5 subplots in Matplotlib

查看:35
本文介绍了在Matplotlib中定位5个子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定位5个子图,以使顶部有3个,底部有2个,但彼此相邻.当前代码接近但我希望最终结果如下所示(忽略灰线):

I would like to position 5 subplots such that there are three of top and two at the bottom but next to each other. The current code gets close but I would like the final result to look like the following (ignore gray lines):

import matplotlib.pyplot as plt

ax1 = plt.subplot(231)
ax2 = plt.subplot(232)
ax3 = plt.subplot(233)
ax4 = plt.subplot(234)
ax5 = plt.subplot(236)

plt.show()

推荐答案

当你使用 suplot2grid 而不是 subplot 时,你可以使用 colspan.

You can use colspan When you use suplot2grid instead of subplot.

import matplotlib.pyplot as plt

ax1 = plt.subplot2grid(shape=(2,6), loc=(0,0), colspan=2)
ax2 = plt.subplot2grid((2,6), (0,2), colspan=2)
ax3 = plt.subplot2grid((2,6), (0,4), colspan=2)
ax4 = plt.subplot2grid((2,6), (1,1), colspan=2)
ax5 = plt.subplot2grid((2,6), (1,3), colspan=2)

然后每个子图需要2列宽,以便第二行中的子图可以移动1列.

And then every subplot needs to be 2 cols wide, so that the subplots in the second row can be shifted by 1 column.

这篇关于在Matplotlib中定位5个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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