matplotlib on mac doesn't display - python

There are a lot of questions about installing matplotlib on mac, but as far as I can tell I've installed it correctly using pip and it's just not working. When I try and run a script with matplotlib.pyplot.plot(x, y) nothing happens. No error, no nothing.
import matplotlib.pyplot
x = [1,2,3,4]
y = [4,3,2,1]
matplotlib.pyplot.plot(x, y)
When I run this in the terminal in a file called pyplot.py I get this:
pgcudahy$ python pyplot.py
pgcudahy$
No errors, but no plot either. In an interactive python shell I get this:
>>> import matplotlib
>>> print matplotlib.__version__
1.1.1
>>> print matplotlib.__file__
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.pyc
Which leads me to believe it's installed correctly.
Any ideas?

You need to call the show function.
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [4,3,2,1]
plt.plot(x, y)
plt.show()

It's likely that the plot is hidden behind the editor window or the spyder window on the screen. Instead of changing matplotlib settings, just learn the trackpack gestures of the mac, "app exposé" is the one you need to make your plots visible (see system preferences, trackpack). Then click on the figure to raise it to the front.

Related

No plot shows up from VS Code

I initiated the VS Code window through a WSL Ubuntu terminal such as
/mnt/d/github/python/python_for_data_science$ code .
And test a plot
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5, 11)
y = x**2
# functional
plt.plot(x, y)
plt.show()
By Run Python File in Terminal, it shows
/mnt/d/github/python/python_for_data_science$ /usr/bin/python /mnt/d/github/python/python_for_data_science/section_8_matplotlib.py
But I didn't see a plot. Have I missed something?
WSL might not have the ability to show UI. Alternatively you can run the code in the Python Interactive window in order to see the output. Try right-click and select Run Current File in Python Interactive window.

Matplotlib does not show labels or numbers

My plots in matplotlib don't show more than one letter/digit.
I do need to use matplotlib from the anaconda folder which is why I'm including sys.path at the beginning. (When I don't include the first 2 lines everything works fine since I have a different matplotlib on my computer. Both are up to date.) Any ideas what I can do to make it work? I'm using Ubuntu and Sublime Text.
Here's my simple code:
import sys
sys.path.insert(0, "/home/mariusz/anaconda/lib/python2.7/site-packages")
import matplotlib.pyplot as plt
grade_fast = [1,7,3,5,6,7,10,11,12]
bumpy_fast = [0,2,3,5,6,7,10,11,12]
grade_slow = [0,2,3,5,6,7,10,11,12]
bumpy_slow = [0,2,3,5,6,7,10,11,12]
plt.scatter(bumpy_fast, grade_fast, label="f")
plt.scatter(grade_slow, bumpy_slow, label="slow")
plt.legend()
plt.xlabel("bumpiness")
plt.ylabel("g")
plt.show()
I re-installed matplotlib, numpy, scikit-learn and scipy and it worked :)
I don't know why though.

matplotlib plot doesn't appear even after editing the backend and using pylab.show()

I have a simple script to test a plot in matplotlib but no window showing the figure appears. On reading other questions on stackoverflow, I've done the following to resolve this:
installed PySide using these instructions.
edited matplotlibrc file with these two lines:
backend : Qt4Agg
#backend.qt4 : PySide # PyQt4 | PySide
so that the command python -c 'import matplotlib; import matplotlib.pyplot; print(matplotlib.backends.backend)' now yields Qt4Agg whereas before it gave agg
included the pylab.show() command. So the set of commands that I now tried in the python interpreter after installing Pyside, and editing the matplotlibrc file look like this:
import pylab
pylab.ion()
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x7fcef627cdd0>]
pylab.show()
However, the plot still doesn't show. Could anyone please help me with this? I am using Ubuntu 14.04 in VirtualBox with python2.7.
When I use your code the plot actually flashes on the screen, but closes immediately. Placing an input() function at the end might help you with debugging it:
import pylab
import matplotlib.pyplot as plt
import numpy as np
pylab.ion()
x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
pylab.show()
tin = input("Test Input: ")
And removing the pylab.ion() actually keep the plot on the screen. This gives you another hint. There are already some good answers why this is happening. E.g.:
Matplotlib ion() function fails to be interactive

matplotlib.pyplot.draw() and matplotlib.pyplot.show() have no effect

In the past I was able to do simple animations with matplotlib with a for loop, but this hasn't worked for some time now.
The standard answer is that you have to turn interactive mode on and/or force a redraw with matplotlib.pyplot.draw(). Here is my minimal working example:
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as mplot
mplot.ion()
fig = mplot.figure(1)
ax = fig.add_subplot(111)
for ii in np.arange(0,10):
x = 200*np.random.rand(30)
ax.plot(x)
mplot.draw()
filename = ("img_%d.png" % ii)
mplot.savefig(filename)
When I run this in Interactive Python Editor, I get one figure at the very end with all the plots in it (this also happens with mplot.show())
When I run this in IPython 3.1 (with Python 3.3.5) from the command line, I get nothing at all.
The mplot.savefig(filename) line does seem to work, as the images are generated.
(It's possible this is a bug in the Qt4 backend.)
Try deleting the line matplotlib.use('Qt4Agg'). Works for me. Also works with matplotlib.use('TkAgg'). So it is a backend problem. There is another way to do animations.

make matplotlib plotting window pop up as the active one

I'm working with python and matplotlib on mac os x.
When I'm working on many different windows and I have to run a script which produces a plot, the plot window always open behind the active window and is very frustration having to switch between windows for looking at the image.
Is it any why to decide the location of the plot window, and/or pop up it as foreground window?
thanks
For me (OSX 10.10.2, Matplotlib 1.4.3), what works is changing the matplotlib backend to TkAgg. Before importing pyplot or anything, go:
import matplotlib
matplotlib.use('TkAgg')
Plot windows now pop-up, and can be Command-Tab'ed to.
I was bothered by exactly the same problem. I found finally a solution (in pylab mode, with qt4agg backend):
get_current_fig_manager().window.raise_()
or
fig = gcf()
fig.canvas.manager.window.raise_()
Regards,
Markus
I found this solution was so often needed (e.g. when using Spyder IDE), I wrapped it into a function.
def show_plot(figure_id=None):
if figure_id is None:
fig = plt.gcf()
else:
# do this even if figure_id == 0
fig = plt.figure(num=figure_id)
plt.show()
plt.pause(1e-9)
fig.canvas.manager.window.activateWindow()
fig.canvas.manager.window.raise_()
I found a good answer on this thread:
How to make a Tkinter window jump to the front?
Basically, the idea is to use window attributes - set the '-topmost' attribute to True (1) to make the window come to the foreground, and then set it to False (0) so that it later allows other windows to appear in front of it. Here's code that worked for me:
import matplotlib.pyplot as plt
wm = plt.get_current_fig_manager()
wm.window.attributes('-topmost', 1)
wm.window.attributes('-topmost', 0)
For MacOS Sierra and python 3.6, Matplotlib 2.0.0
import matplotlib.pyplot as plt
plt.get_current_fig_manager().show()
the above line does the job no need of anything else.
This worked for me!!
(Tested on Mac OS X 10.11, Spyder 2.3.5.2 - Python 3.4)
Go to Preferences > IPython console > Graphics and set a backend to Qt (after that you need to restart the kernel).
Make a file that contains:
def raise_window(figname=None):
if figname: plt.figure(figname)
cfm = plt.get_current_fig_manager()
cfm.window.activateWindow()
cfm.window.raise_()
and import it at startup (Preferences > IPython console > Startup > Run a file). Now, just call function raise_window() below your code.
Example:
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.figure()
plt.plot(X, C)
plt.plot(X, S)
raise_window()
For me only the following works (with TkAgg backend):
plt.gcf().canvas.get_tk_widget().focus_force()
As of matplotlib 1.5.1 on MacOSX 10.11.6, if you start an iPython (5.0.0, Python: 3.5.2) shell and use %matplotlib you can bring a matplotlib plot to the front using:
>>> %matplotlib
Using matplotlib backend: MacOSX
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,3,2,4])
>>> plt.show()
** Edit: Advice seems to be not to use %pylab as it pollutes the global name space **
.. shell and use %pylab you can bring a matplotlib plot to the front using:
>>> %pylab
Using matplotlib backend: MacOSX
Populating the interactive namespace from numpy and matplotlib
>>> plot([1,3,2,4])
>>> show()
You can set
backend : MacOSX
in your matplotlibrc file for a permanent solution.
It works for me on macos mojave, with matplotlib 2.1.2. However, other users have complained that it does not work for them, so it might be affected by other settings
The following worked on Jupyter notebook with Qt5 backend on Windows. I tested it with Python 3.7, matplotlib 3.2.1.
%matplotlib qt5
import matplotlib.pyplot as plt
import numpy as np
from PyQt5 import QtCore
plt.plot(np.linspace(0,1))
window = plt.get_current_fig_manager().window
window.setWindowFlags(window.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
plt.show()
window.setWindowFlags(window.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
plt.show()

Categories

Resources