绘图和轴之间的Matplotlib填充 [英] Matplotlib padding between plot and axis

查看:39
本文介绍了绘图和轴之间的Matplotlib填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个 matplolib 图,在轴和实际图之间有一些填充.

I am trying to do a matplolib figure with some padding between the axis and the actual plot.

这是我的示例代码:

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()

这就是我想要得到的:

推荐答案

在您的情况下,最简单的方法是使用 ax.margins(some_percentage) 或等效的 plt.margins(some_percentage).

In your case, it's easiest to use ax.margins(some_percentage) or equivalently 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()

这篇关于绘图和轴之间的Matplotlib填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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