我有一个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()
File "", line 17, in plt.savefig(str(item))
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 689, in savefig res = fig.savefig(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py", line 2094, in savefig self.canvas.print_figure(fname, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2006, in print_figure canvas = self._get_output_canvas(format)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 1948, in _get_output_canvas .format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))
ValueError: Format '027619\n\n[19920 rows x 15 columns]' is not supported (supported formats: eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff)
根据您得到的错误,问题是由于保存了具有未知扩展名的图像。
因此,只需在
plt.savefig(str(item))
中添加一个扩展名('jpg','png',…)就可以解决这个问题了。在编辑:
由于
list_to_plot
包含数据帧,并且根据我们在评论中讨论的内容,我建议如下:用数据帧名称创建另一个列表,则解决方案如下:
相关问题 更多 >
编程相关推荐