Matplotlib plots not showing up in Mac OSX?

PythonMacosMatplotlibPlot

Python Problem Overview


I am running Mac OSX 10.5.8. I installed matplotlib using macports. I get some examples from the matplotlib gallery like this one, without modification:

http://matplotlib.sourceforge.net/examples/api/unicode_minus.html

I run it, get no error, but the picture does not show up. In Linux Ubuntu I get it.

Do you know what could be wrong here?

Python Solutions


Solution 1 - Python

I had the same problem, even I could see how a new application window was created and immediately disappeared.

Simple solution - just check if you have

# Assumes you have imported "matplotlib.pyplot" as "plt"
plt.show()

after the plot

Solution 2 - Python

I can verify this on my end as well. To fix, here's what I did

sudo port install py25-matplotlib +cairo+gtk2
sudo port install py26-matplotlib +cairo+gtk2

Also, we need to change the default backend to a GUI based one.

Edit the file ~/.matplotlib/matplotlibrc, and add:

backend: GTKCairo

Also, you can try the following, which may allow you to not need the GTK or Cairo backends. Edit ~/.matplotlib/matplotlibrc and add:

backend: MacOSX

With the port with those variants installed, this works as well, but it doesn't require X11.


By the way, the error that I saw was the following:

/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/__init__.py:41: UserWarning: 
Your currently selected backend, 'Agg' does not support show().
Please select a GUI backend in your matplotlibrc file ('/Users/wlynch/.matplotlib/matplotlibrc') or with matplotlib.use()
(backend, matplotlib.matplotlib_fname()))

Solution 3 - Python

This is what worked for me. I just changed the import of Matplotlib

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

Solution 4 - Python

When you try

plt.savefig('myfilename.png')

instead of

plt.show()

does that save the correct image named myfilename.png in the current path?

Solution 5 - Python

After the plot simply add -

plt.show()

The reason this works is to do with interactive vs non-interactive mode. If the backend is opened in non-interactive mode, plt.show() is required at the end of the code chunk. You can check the status by calling plt.isinteractive() and toggle the status using plt.ion() and plt.ioff()

Solution 6 - Python

just to add a note,

The matplotlibrc file was not present on my system and I had to to download a copy from the matplotlib website. Future users may have to do the same.

Solution 7 - Python

This is what worked for me:

brew install pkg-config
brew link pkg-config
brew install pygtk
brew install freetype
brew install libpng

sudo ln -s /usr/local/Cellar/freetype/*/lib/pkgconfig/freetype2.pc /usr/local/lib/pkgconfig/freetype2.pc

git clone [email protected]:matplotlib/matplotlib.git
cd matplotlib
python setup.py build
python setup.py install

References:

http://blog.caoyuan.me/2012/08/matplotlib-error-mac-os-x/ http://matplotlib.org/faq/installing_faq.html#install-from-git http://www.tapir.caltech.edu/~dtsang/python.html

Solution 8 - Python

I only had python 2.5 and I did not want to install python 2.6 on my mac. So I used different procedure mentioned in the following link to solve this problem:

http://www.gtkforums.com/viewtopic.php?f=3&t=54928

What that one actually needs is the following steps:

  1. Searching where is the directory "pygtk-2.0.pc" and locate it. For example mine was located in the following directory:

/opt/local/lib/pkgconfig

  1. Adding the path information to envirement variable. For example:

    PKG_CONFIG_PATH=/opt/local/lib/pkgconfig export PKG_CONFIG_PATH

  2. Download the configuration information file "matplotlibrc" from matplotlib website http://matplotlib.sourceforge.net/_static/matplotlibrc

  3. Change backend to MacOSX in the file and save it

  4. Copy the file to directory .matplotlib You can locate the directory in python by the following command:

    import matplotlib matplotlib.get_configdir()

Solution 9 - Python

Mac comes with its own python (read from here, which is not the best), I would suggest just a clean install of some Python 3.7 or so along with Anaconda and then introduce them as interpreters to PyCharm. anything will work fine and you wont need to add ad-hoc solutions like "backend: MacOSX" or so.

Solution 10 - Python

I wanna share this workable solution for me,

import matplotlib
import platform
if platform.system() == 'Darwin':
    matplotlib.use('MacOSX')

Solution 11 - Python

Do the following if anyone is using spyder.

1.) Start Spyder 2.3.5.2 from Anaconda Launcher 2.) Go to preferences -> IPython console -> Graphics -> Backend: changed it to "Automatic" 3.) Select "Apply" and close preferences 3.) Restart IPython kernel 4.) Create simple graphic like

Solution 12 - Python

As a temporary work around one can save the figure to a .png/.jpg/.pdf and make use of that file for the moment.

## assuming price is out DataFrame that contains columns that we want to plot 
pdf_plot=price.plot().get_figure()  
pdf_plot.savefig('Stocks.pdf')

Solution 13 - Python

sudo port install py37-matplotlib +cairo+gtk3
~/.matplotlib/matplotlibrc used 
backend: MacOSX

Seemed to work on MacOS Mojave 10.14.4 with python 3.7 on the unicode_minus.py example above.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOpen the wayView Question on Stackoverflow
Solution 1 - PythonlayserView Answer on Stackoverflow
Solution 2 - PythonBill LynchView Answer on Stackoverflow
Solution 3 - PythonricardokriegView Answer on Stackoverflow
Solution 4 - PythonChristopheDView Answer on Stackoverflow
Solution 5 - PythonnphadkeView Answer on Stackoverflow
Solution 6 - PythonNeilView Answer on Stackoverflow
Solution 7 - PythonxgMzView Answer on Stackoverflow
Solution 8 - PythonReshad HosseiniView Answer on Stackoverflow
Solution 9 - PythonHassan BahalooView Answer on Stackoverflow
Solution 10 - PythonCasper ChangView Answer on Stackoverflow
Solution 11 - PythonAditya GaonkarView Answer on Stackoverflow
Solution 12 - PythonKarthikView Answer on Stackoverflow
Solution 13 - PythonLocoMoco10View Answer on Stackoverflow