matlibplot:如何在一些子图之间添加空格 [英] matlibplot: How to add space between some subplots

查看:89
本文介绍了matlibplot:如何在一些子图之间添加空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调整某些子图之间的空白?在下面的示例中,假设我要消除第一子图和第二子图之间以及第三子图和第四子图之间的所有空白,并增加第二子图和第三子图之间的空间?

How can I adjust the whitespace between some subplots? In the example below, let's say I want to eliminate all whitespace between the 1st and 2nd subplots as well as between the 3rd and 4th and increase the space between the 2nd and 3rd?

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

f, ax = plt.subplots(4,figsize=(10,10),sharex=True)

ax[0].plot(x, y)
ax[0].set_title('Panel: A')

ax[1].plot(x, y**2)

ax[2].plot(x, y**3)
ax[2].set_title('Panel: B')
ax[3].plot(x, y**4)

plt.tight_layout() 

推荐答案

为了使解决方案接近您的代码,您可以使用创建 5 个子图,其中中间的一个是其他子图的四分之一,然后删除中间的图.

To keep the solution close to your code you may use create 5 subplots with the middle one being one forth in height of the others and remove that middle plot.

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

f, ax = plt.subplots(5,figsize=(7,7),sharex=True, 
                     gridspec_kw=dict(height_ratios=[4,4,1,4,4], hspace=0))

ax[0].plot(x, y)
ax[0].set_title('Panel: A')

ax[1].plot(x, y**2)

ax[2].remove()

ax[3].plot(x, y**3)
ax[3].set_title('Panel: B')
ax[4].plot(x, y**4)


plt.tight_layout()
plt.show()

这篇关于matlibplot:如何在一些子图之间添加空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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