简体   繁体   English

reportlab中的多个matplotlib图

[英]Multiple matplotlib plots in reportlab

I'm trying to put a matplotlib graph onto a reportlab canvas. 我正在尝试将matplotlib图放到reportlab画布上。 I can do a simple graph with the code from this question: How to drawImage a matplotlib figure in a reportlab canvas? 我可以使用以下问题的代码来做一个简单的图形: 如何在Reportlab画布中绘制Image Matplotlib图形? But when I try to use subplots or use multiple plots it will not work properly. 但是,当我尝试使用子图或使用多个图时,它将无法正常工作。 Doing it this way causes the same image to be plotted twice even when I added things like imgdata.close() or deleting the figure: 这样,即使我添加了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)

When trying with subplots it simply produces a blank graph and I did not know where to go with it: 尝试使用子图时,它只会生成一个空白图形,我不知道该去哪里:

    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)

Any solution or advice to this problem would be very much appreciated. 对于这个问题的任何解决方案或建议,将不胜感激。

The key is that you must use plt.close() after you're done adding images. 关键是添加plt.close()图像后必须使用plt.close() Here's a quick example that works for me using seaborn and barplot. 这是一个使用seaborn和barplot对我有用的简单示例。 Assume I have a dataframe with different data that I want plotted over a few figures. 假设我有一个数据框,其中需要将不同的数据绘制在一些图形上。

import matplotlib.pyplot as plt
import seaborn as sns
import cStringIO
from reportlab.platypus import Image

my_df = <some dataframe>
cols_to_plot = <[specific columns to plot]>

plots = []

def create_barplot(col):
    sns_plot = sns.barplot(x='col1', y=col, hue='col2', data=my_df)
    imgdata = cStringIO.StringIO()
    sns_plot.figure.savefig(imgdata, format='png')
    imgdata.seek(0)
    plots.append(Image(imgdata))
    plt.close()                   # This is the key!!!

for col in cols_to_plot:
    create_barplot(col)

for barplot in plots:
    story.append(barplot)

This isn't an ideal solution as it has to save the file as an image instead of using StringIO but it works. 这不是理想的解决方案,因为它必须将文件另存为图像而不是使用StringIO,但是它可以工作。

    import Image as image
    from matplotlib.pyplot import figure
    from reportlab.pdfgen import canvas
    from reportlab.lib.utils import ImageReader

    can = canvas.Canvas()
    self.f = figure()
    self.a = self.f.add_subplot(2,1,1)
    self.a.plot(x,y)
    self.a2 =self.a.twinx()
    self.a2.plot(x,y2,'r')
    self.a2.set_ylabel(ylbl2,color='r')
    self.a.set_xlabel(xlbl)
    self.a.set_ylabel(ylbl,color='b')


    self.f.savefig('plot.png',format='png')
    image.open('plot.png').save('plot.png','PNG')   
    can.drawImage('plot.png',100,250, width=400,height=350)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM