问题描述
我在Mac OS 10.14.4上.我在 miniconda3 环境中安装了 python.下面是带有conda list"
I am on Mac OS 10.14.4. I have python installed in miniconda3 environment. Below is the list of packages with "conda list"
我遇到的问题是,当我在终端中运行"python"并打开外壳程序时,我尝试一个接一个地运行代码.
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.
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的目录中在终端.
The issue I am having is after running plt.show()
the plot window opens showing a white screen without any image in there. 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.
当我运行时,将matplotlib.pyplot导入为plt;python shell中的plt.get_backend()
,我看到'TkAgg'
,现在我尝试更改为 plt.switch_backend('MacOSX')
,因为我看上去有些相似的问题和相似的解决方案.但是,这给了我错误
When I run 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. But, this gives me the error
ImportError:Python未作为框架安装.如果 Python 没有作为框架安装,Mac OS X 后端将无法正常运行.有关在 Mac OS X 上将 Python 作为框架安装的更多信息,请参阅 Python 文档.请重新安装 Python 作为框架,或尝试使用其他后端之一.如果您正在使用 (Ana)Conda,请安装 python.app 并将python"的使用替换为pythonw".有关更多信息,请参见Matplotlib常见问题解答中的在OSX上使用Matplotlib".
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. 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. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
解决这个问题的任何帮助都非常有用
Any help to resolve this issue is much appriciated
推荐答案
我能够解决macOS Majave 10.14.6
的问题,而无需通过添加以下代码通过Anaconda/Conda重新安装(如https://stackoverflow.com/a/56025793/1657354中所述:
I was able to fix the issue with macOS Majave 10.14.6
without having to reinstall via Anaconda/Conda by adding this code (as explained in https://stackoverflow.com/a/56025793/1657354):
import matplotlib
import platform
if platform.system() == 'Darwin':
matplotlib.use('MacOSX')
else:
matplotlib.use('TkAgg')
我相信 platform.system()=='Darwin'
允许此代码在其他平台上工作.
I believe the platform.system() == 'Darwin'
allows this code to work under other platforms.
这篇关于Matplotlib绘图窗口为白色空白,不显示任何图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!