如何设置 Matplotlib 图形的边距?
matplotlibpythondata visualization更新于 2025/6/2 20:28:17
要设置 matplotlib 图形的边距,我们可以使用 margins() 方法。
步骤
- 设置图形大小并调整子图之间和周围的填充。
- 使用 numpy 创建 t 和 y 数据点。
- 在索引 1 处向当前图形添加子图。
- 使用 plot() 方法绘制 t 和 y 数据点。
- 设置图的标题。
- 在索引 2 处向当前图形添加子图。
- 使用 plot() 方法绘制 t 和 y 数据点。
- 设置图的标题绘图。
- 使用 margins(x=0, y=0) 设置绘图边距。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
t = np.linspace(-2, 2, 10)
y = np.exp(-t)
plt.subplot(121)
plt.plot(t, y)
plt.title("Without margins")
plt.subplot(122)
plt.plot(t, y)
plt.title("With x=0 and y=0 margins")
plt.margins(x=0, y=0)
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

