我正在尝试使用一个简单的python程序来显示一个图形。我正在MacCatalina上运行Python 3.9。我的代码是:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1,120,120)
y = (2*10**-5)**2 + ((0.001)/(x))**2
plt.plot(x, y)
plt.show()
我试着按照下面的说明:matplotlib hangs on mac osx and graph is not displayed
也就是说,在matplotlibrc
文件中将Interactive: False
更改为Interactive: True
,这没有任何区别,并替换:
import matplotlib.pyplot as plt
以下是:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
但这也不起作用(我检查了一下图形是否在我的终端后面或其他任何地方丢失了,我找不到它,所以我假设它没有显示出来)
在我的matplotlibrc
文件中,它有backend: Agg
,但当我在运行Python时在终端中键入matplotlib.get_backend()
时,它返回"MacOSX"
。考虑到它在{
## ***************************************************************************
## * BACKENDS *
## ***************************************************************************
## The default backend. If you omit this parameter, the first working
## backend from the following list is used:
## MacOSX Qt5Agg Gtk3Agg TkAgg WxAgg Agg
## Other choices include:
## Qt5Cairo GTK3Cairo TkCairo WxCairo Cairo
## Qt4Agg Qt4Cairo Wx # deprecated.
## PS PDF SVG Template
## You can also deploy your own backend outside of matplotlib by referring to
## the module name (which must be in the PYTHONPATH) as 'module://my_backend'.
#backend: Agg
这是否应该返回"Agg"
我使用pip3下载了numpy和matplotlib,如果可能的话,我不想使用Anaconda,因为我似乎把在那里安装和卸载Windows计算机搞砸了,但这是另一个问题的一部分
当我将代码直接复制并粘贴到运行Python的终端时,我的图形显示出来,我想这解决了我的图形不显示的问题,但是如果我想运行更长的代码段,这将是我的下一步,因为我最终想从matlab导入一个图形,并将其与此图进行比较
如果我更改代码以包含上面链接中的解决方案,则我的代码如下所示:
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
print(matplotlib.get_backend())
import matplotlib.pyplot as plt
x = np.linspace(1,120,120)
y = (2*10**-5)**2 + ((0.001)/(x))**2
plt.plot(x, y)
plt.show()
它输出TkAgg,但仍然不显示我的图形。为什么会发生这种情况,我可以做些什么来修复它,这样我就可以在不复制和粘贴到终端的情况下制作图形
因此,在处理了各种依赖关系、安装brew、安装各种软件包和卸载各种软件包之后,卸载matplotlib并重新安装matplotlib似乎已经解决了问题,现在在终端中运行.py程序时输出图形(例如,终端中的python3 program.py)。我不确定是我安装的任何软件包使它工作了,还是仅仅是卸载和重新安装的过程,但至少现在它工作了。如果有人好奇或者遇到同样的问题,我很乐意在某个时候把我的终端历史记录下来
相关问题 更多 >
编程相关推荐