
调整子图间距可以通过两种方式实现:一是通过`plt.subplots_adjust()`函数;二是通过设置`hspace`和`wspace`参数。
1. 使用`plt.subplots_adjust()`函数
`plt.subplots_adjust()`函数允许你调整子图周围的边距。这个函数接受五个参数,分别是`left`, `right`, `bottom`, `top`和`hspace`。其中,`hspace`用于设置水平方向上的间距,取值范围为0到1,表示相对于子图宽度的间距。
以下是一个简单的例子:
python
import matplotlib.pyplot as plt
# 创建两个子图
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
# 调整子图间距
plt.subplots_adjust(hspace=0.5) # 设置水平方向上的间距为0.5
# 绘制数据
ax1.plot([1, 2, 3], [4, 5, 6])
ax2.plot([1, 2, 3], [7, 8, 9])
# 显示图表
plt.show()
在这个例子中,我们设置了`hspace`为0.5,这意味着两个子图之间的水平间距是每个子图宽度的0.5倍。
2. 使用`hspace`和`wspace`参数
在创建子图时,你可以直接在`plt.subplot()`函数中设置`hspace`和`wspace`参数来调整间距。`hspace`用于设置子图之间的水平间距,而`wspace`用于设置子图与父图之间的水平间距。
以下是一个使用`hspace`和`wspace`的例子:
python
import matplotlib.pyplot as plt
# 创建两个
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv183717