【问题标题】:Python - Saving multiple figures to individual files with a functionPython - 使用函数将多个图形保存到单个文件
【发布时间】:2020-04-20 00:07:40
【问题描述】:

我对 Python 还是很陌生,在大型程序的一部分中使用 pyplot 时遇到了一些问题。我正在将一个列表(取自 csv 的列)传递给一个函数,该函数将绘制一个直方图并将其保存到文件中。这是函数:

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')

在遇到“TypeError: 'tuple' object is not callable”之前,我可以成功地将一张图像保存到文件中。错误本身发生在 lt.hist = plt.hist(column_list, len(column_list), density=True, facecolor='b', alpha=0.75) 我也尝试关闭 plt 并在图上调用 .clf() 。就像我说的,我对 python 很陌生,希望这不是一个愚蠢的错误。感谢您的帮助!

【问题讨论】:

  • 你在哪一行得到这个错误?您发布的代码对我来说似乎没问题..您能否通过编辑问题添加更多信息
  • plt.hist = plt.hist(column_list, len(column_list), density=True, facecolor='b', alpha=0.75) 是发生错误的行,但请注意,只有在一个显示图像已经保存后才会发生。
  • “[我]希望这不是一个愚蠢的错误” 哦,好吧,这是一个愚蠢的错误……plt.hist = plt.hist(... 意味着你用元组覆盖了方法plt.hist方法的第一次调用返回的各种数据。还有,后来fig = plt有点无意义,意思是“从现在开始,figmatplotlib.pyplot模块的别名”
  • 谢谢你,这很有帮助。我会很好的,如果它修复了,我会在下面发布。

标签: python matplotlib


【解决方案1】:

根据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')

【讨论】:

    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多