如何在子图之间创建空间? [英] How to create space between subplots?

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

问题描述

标题几乎说明了一切. 我有一个包含两个子图的笔记本,想在它们之间创建一些空间. 他们说的话彼此看起来太接近了.

解决方案

使用 matplotlib.Figure.subplots_adjust :

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.plot([1,2,3], [1,2,3])
ax2.plot([1,2,3], [3,2,1])
plt.show()

使用wspace参数可以增加宽度:

... # same setup as before
fig.subplots_adjust(wspace=2)
plt.show()

如果要进一步控制axes的位置,则可以将每个轴的偏移量(底部和左侧)和延伸量(宽度和高度)指定为图形的百分比. /p>

需要一些计算才能正确:

import matplotlib.pyplot as plt

# All have the same lower border, height and width, only the distance to
# the left end of the figure differs
bottom = 0.05
height = 0.9
width = 0.15  # * 4 = 0.6 - minus the 0.1 padding 0.3 left for space
left1, left2, left3, left4 = 0.05, 0.25, 1 - 0.25 - width, 1 - 0.05 - width

rectangle1 = [left1, bottom, width, height]
rectangle2 = [left2, bottom, width, height]
rectangle3 = [left3, bottom, width, height]
rectangle4 = [left4, bottom, width, height]

# Create a 8 x 8 (quadratic) figure
plt.figure(1, figsize=(8, 8))

// Create 4 axes their position and extend is defined by the rectangles
ax1 = plt.axes(rectangle1)
ax2 = plt.axes(rectangle2)
ax3 = plt.axes(rectangle3)
ax4 = plt.axes(rectangle4)

# Let's display something in these axes.
ax1.plot([1,2,3,4])
ax2.plot([4,3,2,1])
ax3.plot([4,3,2,1])
ax4.plot([1,2,3,4])

plt.show()

The title pretty much says it all. I have a notebook containing two subplots and would like to create some space between them. They look too close to one another per say.

解决方案

With matplotlib.Figure.subplots_adjust:

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.plot([1,2,3], [1,2,3])
ax2.plot([1,2,3], [3,2,1])
plt.show()

increasing the width can be done with the wspace parameter:

... # same setup as before
fig.subplots_adjust(wspace=2)
plt.show()

If you want to have even more control over the positioning of the axes then you can specify the offset (bottom and left) and the extend (width and height) of each of the axes as percentages of the figure.

It involves a bit of calculation to get right though:

import matplotlib.pyplot as plt

# All have the same lower border, height and width, only the distance to
# the left end of the figure differs
bottom = 0.05
height = 0.9
width = 0.15  # * 4 = 0.6 - minus the 0.1 padding 0.3 left for space
left1, left2, left3, left4 = 0.05, 0.25, 1 - 0.25 - width, 1 - 0.05 - width

rectangle1 = [left1, bottom, width, height]
rectangle2 = [left2, bottom, width, height]
rectangle3 = [left3, bottom, width, height]
rectangle4 = [left4, bottom, width, height]

# Create a 8 x 8 (quadratic) figure
plt.figure(1, figsize=(8, 8))

// Create 4 axes their position and extend is defined by the rectangles
ax1 = plt.axes(rectangle1)
ax2 = plt.axes(rectangle2)
ax3 = plt.axes(rectangle3)
ax4 = plt.axes(rectangle4)

# Let's display something in these axes.
ax1.plot([1,2,3,4])
ax2.plot([4,3,2,1])
ax3.plot([4,3,2,1])
ax4.plot([1,2,3,4])

plt.show()

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

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