Matplotlib的绘图窗口是白色的空白,没有显示任何图像

0 人关注

我使用的是Mac OS 10.14.4。我在miniconda3环境下安装了python。下面是带有 "conda list "的软件包列表enter image description here

enter image description here

enter image description here

我遇到的问题是,当我在终端运行 "python "并打开shell时,我试图一个一个地运行这些代码。

import openmc
sp1 = openmc.StatePoint('statepoint.550-20.h5')
tally1 = sp1.tallies[1]
flux1 = tally1.mean.ravel()
import matplotlib.pyplot as plt
import numpy as np
y = np.reshape(flux1, (200,200)) 
plt.imshow(y, cmap=plt.cm.viridis)
plt.show()

我遇到的问题是,在运行plt.show()后,绘图窗口打开,显示一个白色的屏幕,里面没有任何图像。现在,如果我运行plt.savefig('19.7fast.png')而不是plt.show(),我可以将图像保存在我在终端运行python shell的目录中。

当我在Python shell中运行import matplotlib.pyplot as plt; plt.get_backend()时,我看到了'TkAgg',现在我试着改成plt.switch_backend('MacOSX'),因为我看了一些类似的问题和类似的解决方案。但是,这给了我一个错误

ImportError。Python 没有作为一个框架安装。如果 Python 没有作为框架安装,Mac OS X 后台将无法正常工作。关于在 Mac OS X 上将 Python 作为框架安装的更多信息,请参阅 Python 文档。请重新将Python作为框架安装,或者尝试其他的后端。如果你使用的是(Ana)Conda,请安装python.app并将 "python "改为 "pythonw"。更多信息请参见Matplotlib FAQ中的'Working with Matplotlib on OSX'。

请提供任何帮助以解决这一问题。

4 个评论
开始时比较简单。你能用matplotlib显示任何图形吗?比如python -c "import matplotlib.pyplot as plt; plt.plot([1,3,2]); plt.show()
正如我所说,plt.show()打开的是白色空白的窗口,那里没有图像。见于imgur.com/PmUzLtB谢谢。
刚才有人报告了一个类似的问题。stackoverflow.com/questions/56023918/...
我不觉得TkAgg在MacOS上特别健壮,它似乎会以奇怪的方式失败。 如果你真的想要macOS的后端,你将需要conda install python.app并运行pythonw而不是python。 然而,大多数matplotlib开发者只是使用pyqt,它应该调用qt5Agg后端。
python
python-3.x
matplotlib
miniconda
Sharif Abu Darda
Sharif Abu Darda
发布于 2019-05-07
2 个回答
user1657354
user1657354
发布于 2019-05-07
已采纳
0 人赞同

我能够解决macOS Majave 10.14.6的问题,而不需要通过Anaconda/Conda重新安装,方法是添加这段代码(如在文章中解释的那样https://stackoverflow.com/a/56025793/1657354):

import matplotlib
import platform
if platform.system() == 'Darwin':
    matplotlib.use('MacOSX')
else:
    matplotlib.use('TkAgg')

我相信platform.system() == 'Darwin'允许这段代码在其他平台下工作。

谢谢!解决了我在M1,蒙特雷机器上的问题。
谢谢你也解决了我在Linux上的问题!
Nick Martin
Nick Martin
发布于 2019-05-07
0 人赞同

根据我的经验,要在Mac OS上运行,我发现使用完整版的Anaconda和使用VS Code作为编辑器/IDE要容易得多。

卸载Miniconda。

  • Open a terminal window and remove the entire miniconda install directory:
rm -rf ~/miniconda.
  • You can also edit ```~/.bash_profile`` and remove the miniconda directory from your PATH environment variable
  • Remove the hidden .condarc file and .conda and .continuum directories which are usually created in your directory:
rm -rf ~/.condarc ~/.conda ~/.continuum

安装Anaconda和VS Code。

  • Go to the 安装网站并下载Mac OS的安装程序(我建议选择最新的Python版本(3.7))。

  • 遵循安装说明

  • Once installed open the Anaconda Navigator and select the option to install VS code:

VS代码安装/启动

运行一个测试脚本

  • Open VS Code via the Anaconda Installer
  • Create a new script: File > New File
  • Save it as "test.py"
  • Enter the following code:
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-4*np.pi,4*np.pi)
y = np.sin(x)
plt.plot(x,y,'.-')
plt.show()

  • Save the file
  • Select the Anaconda Python interpreter:
  • Open the command pallet (Ctrl+Shift+P)
  • Enter "Python: Select Interpreter"
  • Choose the one that says "anaconda" in the name...
  • Open the command dialog box again (Ctrl+Shift+P)
  • Enter "Python: Create Terminal" and in terminal run:
python test.py

希望这一切能起作用,你会看到以下情况。

辛函数图

"我在MacOS上遇到了miniconda的问题,最后卸载了所有东西,然后安装了完整版的Anaconda,然后通过桌面集线器安装了VS Code。"你能不能让我知道这个过程的步骤,我问的是如何卸载miniconda和安装Anaconda,最后不知道你说的 "通过桌面集线器安装VS Code "是什么意思?我也在考虑这样做,但我担心会弄乱我目前的环境。但如果你说你已经做了,也许我可以试试。谢谢。
@SharifAbuDarda 如果我更新的答案有效,请你接受?

推荐产品

云服务器_QVM_云主机

云服务器_QVM_云主机

七牛云 QVM 以云服务器为核心,提供含云硬盘、云数据库、高防、负载均衡等解决方案为一体的云计算综合服务。
CDN_内容分发网络

CDN_内容分发网络

七牛 CDN 提供稳定快速的网络访问服务。保障您的音视频点播、大文件下载、应用及 Web 加速服务的稳定及连续性。
云存储_对象存储

云存储_对象存储

七牛云对象存储系统 kodo 支持中心和边缘存储,经过多年大规模用户验证已跻身先进技术行列,并广泛应用于海量数据管理的各类场景。
实时音视频通信_RTC

实时音视频通信_RTC

七牛实时音视频云为您提供一站式解决方案,零基础搭建音视频平台,快速支持视频通话、多人会议、互动直播、语音聊天室等多种业务场景。
页面原文内容由 Sharif Abu Darda、user1657354 等提供。七牛云提供翻译支持。
如您不希望该内容被发布至本网站,请发送邮件至:ug#qiniu.com(邮箱中 # 请改为 @)进行申诉,一经确认,我们将立即删除该内容。