在绘图和轴之间填充Matplotlib

Ant*_*ier 5 python plot matplotlib

我正在尝试使用轴和实际绘图之间的一些填充来做matplolib图.

这是我的示例代码:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1)

x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)

plt.plot(x, y, 'r')
plt.grid(True)


plt.show()
Run Code Online (Sandbox Code Playgroud)

这就是我想要得到的:

带有x和y移位的情节

Joe*_*ton 9

在您的情况下,它最容易使用ax.margins(some_percentage)或等效plt.margins(some_percentage).

例如:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)

ax.plot(x, y, 'r')
ax.grid(True)
ax.margins(0.05) # 5% padding in all directions

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述