在Mac系统中保存matplotlib图形时无法修改文件名



在 Python 中执行 show() 命令后,我只能将数字保存为figure_1.png我无法更改我的文件名。不允许在"另存为:"旁边键入。此时如何启用输入我的文件名?

很抱歉,我至少需要 10 个声誉才能发布屏幕截图。

我遇到了完全相同的问题,我在解决 matplotlib 后端的潜在问题中找到了答案(默认后端"macosx"导致此问题)。解决方案是更改 matplotlibrc 文件中的后端行,您可以通过运行

import matplotlib
matplotlib.matplotlib_fname()

在 Python 终端中。在 matplotlibrc 文件中,查找并更改行

backend      : macosx

到(例如):

backend      : TkAgg

所有的问题都应该消失。它对我有用,我希望它有所帮助。

刚刚找到了一个解决方案:

使用 conda install python.app 安装 pythonw

然后在终端中使用pythonw而不是python

例如

pythonw my_plot.py

希望这有帮助

试试这个。我不知道你是否在使用pyplot,但它很常见。确保不要在代码中的任何位置显示()图形 - 它会阻止savefig()函数工作。这对我有用,并将myfigure.png保存在我运行代码的目录中。

import matplotlib.pyplot as plt
# make a figure with 1 row and 1 column
fig, myplot = plt.subplots( nrows=1, ncols =1)
# simple plot
t = arange(0.0,2.0,0.01)
s = sin(2*pi*t)
myplot = plot(t,s)
# save figure with specified filename
plt.savefig('myfigure.png')
# close figure
plt.close(fig)

对于某些人来说,这个讨论可能会回答这个问题: https://matplotlib.org/faq/osx_framework.html

如果没有 Python 的框架构建,matplotlib 无法在 OS X 上正常运行.Anaconda 不提供框架构建,因此问题......虽然直到最近我才记得这是一个问题!

尽管使用了 Python 3,但我实际上无法使链接中建议的虚拟环境修复对我有用。

最新更新