简体   繁体   中英

Matplotlib plot window is white blank without showing any image

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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