Python 用matplotlib圈出图中的边框

Python 用matplotlib圈出图中的边框,python,matplotlib,Python,Matplotlib,我试图用matplotlib画一个直径为2英寸、边框为10像素的圆,我想把它保存在一个文件中。这是我的代码: import matplotlib.pyplot as plt from matplotlib import patches path = 'test.png' fig1 = plt.figure() fig1.dpi = 100 fig1.set_size_inches(2, 2) ax1 = fig1.add_subplot(111, aspect='equal') ax1.ax

我试图用matplotlib画一个直径为2英寸、边框为10像素的圆,我想把它保存在一个文件中。这是我的代码:

import matplotlib.pyplot as plt
from matplotlib import patches

path = 'test.png'

fig1 = plt.figure()
fig1.dpi = 100
fig1.set_size_inches(2, 2)
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.axes.get_xaxis().set_visible(False)
ax1.axes.get_yaxis().set_visible(False)

ax1.add_patch(patches.Circle((0.5, 0.5),
                             radius=0.5,
                             color='k', linewidth=10, fill=False))

fig1.tight_layout()
fig1.savefig(path, bbox_inches='tight', pad_inches=0)
这就是我得到的:

正如您所看到的,部分边界不在图片中

事实上,即使做一些简单得多的事情,我也会得到类似的结果:

import matplotlib.pyplot as plt
from matplotlib import patches

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.Circle((0.5, 0.5),
                             radius=0.5,
                             color='k', linewidth=10, fill=False))

plt.show()
所以,我不明白问题出在哪里


我做错了什么?

限制很小,默认情况下取所有点的最小位置和最大值,而不考虑厚度,我建议您将限制设置得更大一点。您必须是
{axes}.set_xlim()
{axes}.set_ylim()


添加面片不会自动调整轴限制。您必须调用
ax1.autoscale\u view()
来调整内容的限制

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.Circle((0.5, 0.5),
                             radius=0.5,
                             color='k', linewidth=10, fill=False))
ax1.autoscale_view()

我不知道为什么,这对我不起作用!我已经复制并粘贴了您的代码,但仍然从图像中获取边框。我正在使用python 3.5和matplotlib 1.5.3。@gpSO它与matplotlib 2.0.0一起工作。如果无法升级,则必须按照另一个答案中的说明手动调整轴限制。
plt.autoscale(enable=True)
:“将轴视图自动缩放到数据”是您的朋友。
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.Circle((0.5, 0.5),
                             radius=0.5,
                             color='k', linewidth=10, fill=False))
ax1.autoscale_view()