Python Matplotlib png输出出现;“破碎的”;在reportlab';s pdf

Python Matplotlib png输出出现;“破碎的”;在reportlab';s pdf,python,matplotlib,graphics,reportlab,Python,Matplotlib,Graphics,Reportlab,有人知道当我通过reportlab以PDF格式打印matplotlib绘图时,matplotlib绘图出现“中断”的原因吗 这是我正在使用的Python代码: list = range(6) fig = plt.figure(figsize=(10, 4)) ax = plt.subplot(111) ax.plot(list, [2*x for x in list] , label='y = 2x') ax.plot(list, [0.5*x for x in list], label='

有人知道当我通过reportlab以PDF格式打印matplotlib绘图时,matplotlib绘图出现“中断”的原因吗

这是我正在使用的Python代码:

list = range(6)

fig = plt.figure(figsize=(10, 4))
ax = plt.subplot(111)

ax.plot(list, [2*x for x in list] , label='y = 2x')
ax.plot(list, [0.5*x for x in list], label='y = 0.5*x')
ax.plot(list, list, label='y = x')

plt.xlabel('x')
plt.ylabel('y')
plt.title("Sample Plot")

plt.grid(True, which='major', axis='y')

box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9])

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), ncol=3,
          fontsize='small')

plt.savefig('test.png',dpi=1000) 

c = canvas.Canvas("hello.pdf")
c.drawImage('test.png', 100, 600, width=4 * 100, height=4 * 40)
c.showPage()
c.save()
matplotlib test.png输出看起来很好:

但是,hello.pdf文件中的相同图像看起来非常糟糕:


我想导出png文件时,应该使用与报告中要放置png文件的空间完全相同的尺寸。理想情况下,您可能更愿意包含矢量格式的图像;pdf,但需要更多的代码。还有一个选项。特别是对于我创建的用例[一个名为autobasedoc的小软件包][1],它有文档。我希望你觉得它有用![1]: