【发布时间】:2018-12-07 09:51:57
【问题描述】:
我正在尝试将 matplotlib 图放到 reportlab 画布上。我可以用这个问题的代码做一个简单的图表:How to drawImage a matplotlib figure in a reportlab canvas? 但是当我尝试使用子图或使用多个图时,它将无法正常工作。这样做会导致同一图像被绘制两次,即使我添加了诸如 imgdata.close() 或删除图形之类的东西:
from matplotlib.figure import Figure
import cStringIO
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
can = canvas.Canvas()
self.f = Figure()
plot(x,y)
xlabel(xlbl)
ylabel(ylbl)
imgdata=cStringIO.StringIO()
savefig(imgdata,format='png')
imgdata.seek(0)
Image = ImageReader(imgdata)
can.drawImage(Image,100,250, width=400,height=350)
self.g = Figure()
plot(x,y)
xlabel(xlbl)
ylabel(ylbl)
secondimgdata = cStringIO.StringIO()
savefig(secondimgdata,format='png')
secondimgdata.seek(0)
Image2 = ImageReader(secondimgdata)
can.drawImage(Image2,100,150, width=400,height=350)
当尝试使用子图时,它只会生成一个空白图,我不知道该去哪里:
self.f = Figure()
self.a = self.f.add_subplot(111)
self.a.plot(x,y)
self.a2 =self.a.twinx()
self.a2.plot(x,y2,'r')
self.a2.set_ylabel(ylbl2)
self.a.set_xlabel(xlbl)
self.a.set_ylabel(ylbl)
对此问题的任何解决方案或建议将不胜感激。
【问题讨论】:
标签: python matplotlib tkinter reportlab