Matplotlib plots not showing up in Mac OSX?
开发者_运维技巧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?
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
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()))
This is what worked for me. I just changed the import of Matplotlib
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
When you try
plt.savefig('myfilename.png')
instead of
plt.show()
does that save the correct image named myfilename.png
in the current path?
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()
I wanna share this workable solution for me,
import matplotlib
import platform
if platform.system() == 'Darwin':
matplotlib.use('MacOSX')
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.
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 git@github.com: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
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
2) Adding the path information to envirement variable. For example:
PKG_CONFIG_PATH=/opt/local/lib/pkgconfig
export PKG_CONFIG_PATH
3) Download the configuration information file "matplotlibrc" from matplotlib website http://matplotlib.sourceforge.net/_static/matplotlibrc
4) Change backend to MacOSX in the file and save it
5) Copy the file to directory .matplotlib You can locate the directory in python by the following command:
import matplotlib
matplotlib.get_configdir()
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.
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
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')
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.
conda The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python.
精彩评论