简体   繁体   English

Matplotlib绘图窗口为白色空白,不显示任何图像

[英]Matplotlib plot window is white blank without showing any image

I am on Mac OS 10.14.4. 我在Mac OS 10.14.4上。 I have python installed in miniconda3 environment. 我在miniconda3环境中安装了python。 Below is the list of packages with "conda list" 以下是带有“ conda list”的软件包列表 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

The issue I am having is when I run "python" in the terminal and open the shell I try to run the codes one by one. 我遇到的问题是,当我在终端中运行“ python”并打开外壳时,我尝试一一运行这些代码。

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()

The issue I am having is after running plt.show() the plot window opens showing a white screen without any image in there. 我遇到的问题是运行plt.show() ,绘图窗口打开,显示白色屏幕,其中没有任何图像。 Now if I run plt.savefig('19.7fast.png') instead of plt.show() i can save the image in the directory where I run the python shell in terminal. 现在,如果我运行plt.savefig('19.7fast.png')而不是plt.show()我可以将图像保存在我在终端中运行python shell的目录中。

When I run import matplotlib.pyplot as plt; plt.get_backend() 当我运行import matplotlib.pyplot as plt; plt.get_backend() import matplotlib.pyplot as plt; plt.get_backend() in python shell I see 'TkAgg' , Now I tried changing to plt.switch_backend('MacOSX') , because I looked some similar issue and similar solution. python shell中的import matplotlib.pyplot as plt; plt.get_backend()我看到'TkAgg' ,现在我尝试更改为plt.switch_backend('MacOSX') ,因为我看上去有一些类似的问题和类似的解决方案。 But, this gives me the error 但是,这给了我错误

ImportError: Python is not installed as a framework. ImportError:Python未作为框架安装。 The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. 如果未将Python安装为框架,则Mac OS X后端将无法正常运行。 See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. 有关在Mac OS X上将Python安装为框架的更多信息,请参见Python文档。请重新安装Python作为框架,或尝试其他后端之一。 If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. 如果您正在使用(Ana)Conda,请安装python.app并将“ python”的使用替换为“ pythonw”。 See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information. 有关更多信息,请参见Matplotlib FAQ中的“在OSX上使用Matplotlib”。

Any help to resolve this issue is much appriciated 解决此问题的任何帮助都非常有用

In my experience, to get this working on Mac OS, I found it much easier going with the full version of Anaconda and using VS Code as an Editor/IDE. 以我的经验,要使其在Mac OS上运行,我发现使用完整版本的Anaconda并使用VS Code作为E​​ditor / IDE容易得多。

Uninstall Miniconda: 卸载Miniconda:

  • Open a terminal window and remove the entire miniconda install directory: 打开一个终端窗口,并删除整个miniconda安装目录:
rm -rf ~/miniconda.
  • You can also edit ```~/.bash_profile`` and remove the miniconda directory from your PATH environment variable 您也可以编辑~~ .bash_profile并从PATH环境变量中删除miniconda目录。
  • Remove the hidden .condarc file and .conda and .continuum directories which are usually created in your directory: 删除隐藏的.condarc文件以及通常在您的目录中创建的.conda和.continuum目录:
rm -rf ~/.condarc ~/.conda ~/.continuum

Install Anaconda and VS Code: 安装Anaconda和VS Code:

  • Go to the installation website and download the Mac OS installer (I recommend choosing the latest version of Python (3.7) 转到安装网站并下载Mac OS安装程序(我建议选择最新版本的Python(3.7)

  • Follow the installation instructions 请遵循安装说明

  • Once installed open the Anaconda Navigator and select the option to install VS code: 安装完成后,打开Anaconda Navigator,然后选择安装VS代码的选项:

VS Code Install/Launch VS代码安装/启动

Run a test script 运行测试脚本

  • Open VS Code via the Anaconda Installer 通过Anaconda安装程序打开VS代码
  • Create a new script: File > New File 创建一个新脚本:File> New File
  • Save it as "test.py" 将其另存为“ 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: 选择Anaconda Python解释器:
  • Open the command pallet ( Ctrl+Shift+P ) 打开命令托盘( Ctrl+Shift+P
  • Enter "Python: Select Interpreter" 输入“ Python:选择解释器”
  • Choose the one that says "anaconda" in the name... 选择名称中带有“ anaconda”的名称...
  • Open the command dialog box again ( Ctrl+Shift+P ) 再次打开命令对话框( Ctrl+Shift+P
  • Enter "Python: Create Terminal" and in terminal run: 输入“ Python:创建终端”,然后在终端运行:
python test.py

Hopefully, this will all work and you'll see the following: 希望这一切都会起作用,您将看到以下内容:

Sin function graph 正弦函数图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM