1

I am on Mac OS 10.14.4. I have python installed in miniconda3 environment. Below is the list of packages with "conda list" enter image description here

enter image description here

enter image description here

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

4
  • 2
    Start simpler. Can you show any graph with matplotlib? like python -c "import matplotlib.pyplot as plt; plt.plot([1,3,2]); plt.show()? Commented May 6, 2019 at 22:37
  • As I said plt.show() opens up the window of white blank, no image there. See in imgur.com/PmUzLtB Thanks. Commented May 7, 2019 at 13:58
  • Someone reported a similar problem just now: stackoverflow.com/questions/56023918/… Commented May 7, 2019 at 15:10
  • I don't find TkAgg to be particularly robust on MacOS and it seems to fail in strange ways. If you really want the macOS backend you will need to conda install python.app and run pythonw instead of python. However, most of the matplotlib devs just use pyqt, which should call the qt5Agg backend. Commented May 7, 2019 at 16:36

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Solved my problem on my M1, Monterey machine.
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 Comments

"I've had issues with miniconda on MacOS and ended up uninstalling everything and then installing the full version of Anaconda and then VS Code via the desktop hub." can you please let me know the steps to follow for this process, I am asking like how to uninstall miniconda and install anaconda and finally not sure what you mean by "VS Code via the desktop hub."? I was also thinking of doing that but was afraid I might mess up my current environment. but if you say you have done it, maybe I can give it a go. Thanks.
@SharifAbuDarda If my updated answer works please can you accept?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.