Matplotlib plot window is white blank without showing any image

橙三吉。 提交于 2019-12-13 03:12:47

问题


I am on Mac OS 10.14.4. I have python installed in miniconda3 environment. Below is the list of packages with "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.

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. 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.

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


回答1:


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.

Uninstall 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

Install Anaconda and VS Code:

  • Go to the installation website and download the Mac OS installer (I recommend choosing the latest version of Python (3.7)

  • Follow the installation instructions

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

VS Code Install/Launch

Run a test script

  • 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

Hopefully, this will all work and you'll see the following:

Sin function graph




回答2:


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

I believe the platform.system() == 'Darwin' allows this code to work under other platforms.



来源:https://stackoverflow.com/questions/56013105/matplotlib-plot-window-is-white-blank-without-showing-any-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!