%matplotlib qt5 runs but doesn't generate graphs - python

I was using this code on my Jupyter Notebook until yesterday and it was working fine:
%matplotlib qt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X,Y,Z,c='black')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
This morning, for no apparent reason, it started saying that there was an error with qt4 or something. After some research I installed qt5 and changed the first line of the code to "%matplotlib qt5" which now makes the code run without errors, but doesn't generate any graphs. If I take the 5 after qt I run into the message "Warning: Cannot change to a different GUI toolkit: qt. Using qt5 instead.". I can still plot graphs with "%matplotlib inline", but I wanted some interactivity. Any ideas why this is happening?

It seems something got updated; but with the information available it's not possible to find out what it is. In any case, since you have pyqt5 installed you may use %matplotlib qt5. Then you probably just forgot to type plt.show()?
%matplotlib qt5
import matplotlib.pyplot as plt
plt.plot([1,4,2])
plt.show()
Also make sure to restart the kernel if you change the interactive backend, and if in doubt, let the line %matplotlib qt5 appear before importing pyplot.

Related

figsize does not work for matplotlib 3d plot

I plotted 2 figures, here is the screenshot
figsize
the one (2D) on top is rendered as expected.
the one (3D) on bottom is not, the figure is so small!
setting the value of figsize does not work.
figsize=(3,3) and figsize=(13,13) give the same result!
%matplotlib inline rendered same way.
I tried different browsers, clear the cache, dose not work either.
I guess some rcParams control this, because this is reproducible only on my mac, but I don't have a clue to find it!
any clue will be appreciated.
whole coding:
%pylab inline
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(3,3))
ax = fig.gca(projection='3d')
ax.scatter(0.5, 0.5, 0.5, marker = '^')
As of notebook version 5.7.4, on MacOS 10.14.2 and Chrome 71.0.3578.98, this problem persists but can be fixed with %matplotlib inline setting by using the magic command
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
as given in the documentation here.
Correctly funcitoning example.
It appears to be a bug in Jupyter.
As a quick fix, you can use the notebook- instead of the inline-backend for matplotlib.
To do so, replace %matplotlib inline with %matplotlib notebook.
Upgrading matplotlib from version 3.0.0 to 3.0.2 solved the problem. If you use pip type:
pip install --upgrade matplotlib
I verified the case with Jupyter version 4.4.0.

Pycharm Jupyter Interactive Matplotlib

Interactive matplotlib plotting is already a thing, but does not work properly in Pycharm, when used within a jupyter notebook.
The %matplotlib notebook does not work (throws no error, but I get <IPython ... JavaScript object> instead of a plot. If I plot normally (also with or without plt.show()) I just get a png and cannot interact in any way (even if, e.g., sliders are visible).
I couldn't find any answers elsewhere to this exact problem. It might be working in the browser version of jupyter, but I would like to stick to using PyCharm.
Pycharm v 2017.3 Community Edition
You can try:
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
Instead of importing only the peplos.
It's a trick I found on forum of Jetbrains
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000736584-SciView-in-PyCharm-2017-3-reduces-functionality-of-Matplotlib It works for me. With this, you skip actually the Sciview and plot in a normal matplotlib window

Matplotlib animations do not work in PyCharm

I've found various short files that produce animations using matplotlib. In general, they work fine when run from the command line, but in PyCharm I only get a still frame.
I'm asking the same question as Matplotlib does not update plot when used in an IDE (PyCharm). There's an answer posted there, which seems to work for the original asker. When I run that code from the command line, it works fine. From PyCharm, it pauses for a long time (presumably running the animation) and then shows a still frame (that looks like either the beginning or end of the animation).
I'm running Python 3.6.2 (Anaconda) through PyCharm 2017.3.2 (Professional) on a Mac (OS 10.11.6). I created a Python project in PyCharm, pasted that code into a .py file, installed the appropriate libraries (matplotlib 2.0.2, numpy 1.13.1), and ran the program.
The only difference I can see between this and what I did on the command line is that python --version there gives:
Python 3.6.0 :: Anaconda custom (x86_64)
What else could be the problem?
According to this answer and this ticket, you can disable Show plots in tool window(File->Settings->Tools->Python Scientific) in Pycharm, and I will give an example for this solution.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
plt.show()
The above (or in the link attached) did not work for me, however, i found this works well (running 2.7 with anaconda and ipython console)-
Instead of executing the script normally (using run / Shift+f10), i would first set:
%matplotlib qt5
then execute the script from pycharm console, using
runfile('/path/to/script.py')
resulting in a similar result as if i would do the same from the stand alone ipython console:
(Note - the figure is animated)

Plotting window doesn't show up with pylab [duplicate]

I just installed matplotlib in Ubuntu 9.10 using the synaptic package system.
However, when I try the following simple example
>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]
I get no plot window. Any ideas on how to get the plot window to show?
You can type
import pylab
pylab.show()
or better, use ipython -pylab.
Since the use of pylab is not recommended anymore, the solution would nowadays be
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
pylab.show() works but blocks (you need to close the window).
A much more convenient solution is to do pylab.ion() (interactive mode on) when you start: all (the pylab equivalents of) pyplot.* commands display their plot immediately. More information on the interactive mode can be found on the official web site.
I also second using the even more convenient ipython -pylab (--pylab, in newer versions), which allows you to skip the from … import … part (%pylab works, too, in newer IPython versions).
Try this:
import matplotlib
matplotlib.use('TkAgg')
BEFORE import pylab
The code snippet below works on both Eclipse and the Python shell:
import numpy as np
import matplotlib.pyplot as plt
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# Just print x and y for fun
print x
print y
# Plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
# Without the line below, the figure won't show
plt.show()
Any errors show up? This might an issue of not having set the backend. You can set it from the Python interpreter or from a config file (.matplotlib/matplotlibrc) in you home directory.
To set the backend in code you can do
import matplotlib
matplotlib.use('Agg')
where 'Agg' is the name of the backend. Which backends are present depend on your installation and OS.
http://matplotlib.sourceforge.net/faq/installing_faq.html#backends
http://matplotlib.org/users/customizing.html
Modern IPython uses the "--matplotlib" argument with an optional backend parameter. It defaults to "auto", which is usually good enough on Mac and Windows. I haven't tested it on Ubuntu or any other Linux distribution, but I would expect it to work.
ipython --matplotlib
If you encounter an issue in which pylab.show() freezes the IPython window (this may be Mac OS X specific; not sure), you can cmd-c in the IPython window, switch to the plot window, and it will break out.
Apparently, future calls to pylab.show() will not freeze the IPython window, only the first call. Unfortunately, I've found that the behavior of the plot window / interactions with show() changes every time I reinstall matplotlib, so this solution may not always hold.
If you are starting IPython with the --pylab option, you shouldn't need to call show() or draw(). Try this:
ipython --pylab=inline
--pylab no longer works for Jupyter, but fortunately we can add a tweak in the ipython_config.py file to get both pylab as well as autoreload functionalities.
c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']
If you are user of Anaconda and Spyder then best solution for you is that :
Tools
-->
Preferences
-->
Ipython console
-->
Graphic Section
Then in the Support for graphics (Matplotlib) section:
select two avaliable options
and in the Graphics Backend:
select Automatic
Another possibility when using easy_install is that you need to require the most recent version of matplotlib.
Try:
import pkg_resources
pkg_resources.require("matplotlib")
before you import matplotlib or any of its modules.

Python matplotlib

I'm having trouble getting the window for matplotlib to show.
I've downloaded python 3.3, matplotlib for python 3.3, and numpy.
I've also installed python tools for Visual Studio 2012 so I can create python solutions in that environment.
With all that out of the way... I'm running this EXTREMELY simple script:
import numpy as np
import matplotlib.pyplot as plt
import pylab
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
# without the line below, the figure won't show
pylab.show()
This compiles with no warnings or errors, but only my console window displays; no graph or interactive window ever shows up. I tried running the scrip from the command prompt thinking maybe the visual studio environment was throwing it off, but it still didn't work.
I also tried running with python 2.7 and it also didn't work.
Every tutorial I found confirmed that this should be working. I'm pulling my hair out and would praise any help at this point.
You should type
plt.show()
instead.
You need to put
plt.figure()
to make the figure window before you start plotting information to it
If you are using a GUI console such as Spyder, make sure you have turned on interactive plots so that it plots to a separate window and not inline into the console

Categories