简体   繁体   English

Python - 使用 function 将多个图形保存到单个文件

[英]Python - Saving multiple figures to individual files with a function

I'm pretty new to Python and am experiencing some issues while using pyplot in part of a larger program.我对 Python 很陌生,并且在大型程序的一部分中使用 pyplot 时遇到了一些问题。 I am passing a list (which is taken from a column of a csv) to a function which is to plot a histogram and save it to file.我正在将一个列表(取自 csv 的列)传递给 function,即 plot 一个直方图并将其保存到文件中。 Here is the function:这是 function:

def plot_hist(column_list):
    """Plots a histogram from column data passed as a list"""
    global display_count
    display_count += 1
    plt.hist = plt.hist(column_list, len(column_list), density=True, facecolor='b', alpha=0.75)
    plt.grid(True)
    fig = plt
    fig.savefig(f'../wk5/display{display_count}.png')

I can successfully save one image to file before experiencing "TypeError: 'tuple' object is not callable".在遇到“TypeError: 'tuple' object is not callable”之前,我可以成功地将一张图像保存到文件中。 The error itself occurs at lt.hist = plt.hist(column_list, len(column_list), density=True, facecolor='b', alpha=0.75) I also have tried closing the plt and calling.clf() on the fig.错误本身发生在lt.hist = plt.hist(column_list, len(column_list), density=True, facecolor='b', alpha=0.75)我也尝试关闭 plt 并在图上调用.clf() . Like I said, i'm pretty green to python and hope it's not a stupid mistake.就像我说的,我对 python 很满意,希望这不是一个愚蠢的错误。 I appreciate any help!我很感激任何帮助!

Based on the input from gboffi , I got rid of some useless assignments and it works like a charm.根据gboffi的输入,我摆脱了一些无用的任务,它就像一个魅力。

def plot_hist(column_list):
    """Plots a histogram from column data passed as a list"""
    global display_count
    display_count += 1
    plt.hist(column_list, len(column_list), density=True, facecolor='b', alpha=0.75)
    plt.grid(True)
    plt.savefig(f'../wk5/display{display_count}.png')

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

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