将 Matplotlib 中的多个 .png 图形输出到 Python 3.4 中的一个 zip 文件 [英] Outputing multiple .png graphs in Matplotlib to one zip file in Python 3.4

查看:38
本文介绍了将 Matplotlib 中的多个 .png 图形输出到 Python 3.4 中的一个 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序使用 Python 中的 MatPlotLib 从 CSV 文件输出多个不同的饼图(超过 60 个).我认为不需要共享所有代码,但是我有一个 draw()函数来创建图形,并以以下内容结尾:

I wrote a program that outputs multiple different pie charts (more than 60) from a CSV file using MatPlotLib in Python. I don't think I need to share all of the code, but I have a draw() function that creates the graph, that ends with the following:

def draw(data):
    [make the graph]
    plt.savefig(filename)

此函数成功地将我的一个图形制作为 cwd 中的 .png 文件.然后我运行一个循环,为我的数据的每个部分创建一个饼图.代码如下:

This function successfully makes one of my graphs as a .png file in the cwd. I then run a loop that creates a pie chart for each section of my data. The code is as follows:

if __name__ == '__main__':
    data = sys.argv[1]
    things = process(data) 
    for thing in things.values():
        draw(thing)

我想知道是否有任何 Python 包可以帮助我将所有 60 多个图形自动输出到一个 zip 文件中.

I'm wondering if there are any Python packages that would help me automatically output all 60+ graphs to one zip file.

推荐答案

当然,有标准的 zip文件模块,可以压缩现有文件,也可以压缩一串字节.因此,如果您不想单独创建文件然后压缩它们,可以将它们保存到 BytesIO对象,然后压缩这些字节缓冲区.我已经将您链接到Python 3文档,但是这些功能在Python 2中也可用.

Sure, there's the standard zipfile module, which can zip existing files, and it can also zip a string of bytes. So if you don't want to create the files separately and then zip them you could save them to BytesIO objects and then zip up those byte buffers. I've linked you to the Python 3 docs, but those features are also available in Python 2.

这篇关于将 Matplotlib 中的多个 .png 图形输出到 Python 3.4 中的一个 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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