【问题标题】:How to pass a list variable to a matplotlib plt.savefig argument如何将列表变量传递给 matplotlib plt.savefig 参数
【发布时间】:2019-12-10 21:43:42
【问题描述】:

我有一个 for 循环,可以在每个循环中保存一个绘图。我希望我的列表的字符串名称是 savefig 文件名。但是,Savefig 需要文件名路径或文件名 + 格式。

我正在努力将列出的变量字符串作为文件名传递。 Savefig 推断数据框本身而不是字符串名称。建议克服非常感谢。

最终我希望我的图表被命名为苹果和香蕉(见下文)。

我在 for 循环中尝试了以下方法,但都返回了错误。

#plt.savefig(str(item))
#plt.savefig("Graph" + str(item) +".png", format="PNG")
#plt.savefig('Graph_{}.png'.format(item))   
#plt.savefig(item, format='.jpg')

apples = df_final[(df_final['Timetag [UTC]'] > '21/12/2018  13:28:00') & 
(df_final['Timetag [UTC]'] <= '21/12/2018  19:00:00')]

bananas = df_final[(df_final['Timetag [UTC]'] > '21/12/2018  17:28:00') & 
(df_final['Timetag [UTC]'] <= '21/12/2018  21:00:00')]


List_to_plot = [apples, bananas]


for item in List_to_plot:
    item.plot(y='Delta Port/STBD', label='Sway')
    plt.savefig(str(item))
    plt.show()
    plt.clf()

文件“”,第 17 行,在 plt.savefig(str(item))

文件 "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", 第 689 行,在 savefig 中 res = fig.savefig(*args, **kwargs)

文件 "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py", 第 2094 行,在 savefig 中 self.canvas.print_figure(fname, **kwargs)

文件 "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", 第 2006 行,在 print_figure 中 canvas = self._get_output_canvas(格式)

文件 "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", 第 1948 行,在 _get_output_canvas .format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))

ValueError: 格式 '027619\n\n[19920 行 x 15 列]' 不是 支持(支持的格式:eps、jpeg、jpg、pdf、pgf、png、ps、raw、 rgba, svg, svgz, tif, tiff)

【问题讨论】:

  • 例如将plt.savefig(str(item)) 替换为plt.savefig(str(item) + '.jpg')。问题是由于保存了未知扩展名的图像
  • 我之前尝试过,但是 savefig 将我的项目读取为数据框,而不是变量的字符串(苹果、香蕉)。
  • 你试过后得到什么回报?
  • 文件 "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py",第 1966 行,保存 fp = builtins.open(filename, "w+b") FileNotFoundError :[Errno 2]没有这样的文件或目录:'时间标签[UTC] UTMposMeasN1 [] ... pitch1 [°] heave1 [m]\n6892822 2018-12-21 13:28:01 10014875.49
  • ^ savefig 正在尝试使用我的数据框本身保存,而不是我想要的变量名称(苹果香蕉)

标签: python pandas dataframe matplotlib


【解决方案1】:

根据您遇到的错误,问题是由于保存了未知扩展名的图像。
因此,只需将扩展名('jpg','png',...)添加到plt.savefig(str(item)),即可解决此问题,即plt.savefig(str(item) + '.jpg')

编辑:
由于list_to_plot 包含数据框,并且根据我们在评论中讨论的内容,我建议如下:
使用数据框名称创建另一个列表,然后解决方案如下:

List_to_plot = [apples, bananas]
names = ['apples', 'bananas']
# loop over the element in the list_to_plot
for elt in (0, 1):
    # loop over the length of each dataframe to get the index of the image
    for i in range(list_to_plot[elt].shape[0]):
        # do your processing
        item.plot(y='Delta Port/STBD', label='Sway')
        # save the image with the appropriate index
        plt.savefig(names[elt] + '{}.jpg'.format(i))
        plt.show()
        plt.clf()

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2018-11-29
    • 2015-01-05
    • 2018-06-07
    • 1970-01-01
    相关资源
    最近更新 更多