pyplot animation example code won't animate - python

I'm trying to come up to speed on the animation functionality in pyplot. I've grabbed the example code given here: https://matplotlib.org/2.0.0/examples/animation/animate_decay.html
and I'm attempting to run it myself. The result I get is empty axes with no animation at all. My setup is anaconda 4.3, with python 3.6. Matplotlib is version 2.0.0, and I'm running this in a jupyter notebook. I've tried IE 11, Chrome 59 and Firefox 54 on both a windows 7 machine and a Mac.

If you're using Spyder, you need to change your Matplotlib backend by going to the menu
Tools > Preferences > IPython console > Graphics
and then selecting Automatic in the section called Graphics backend. After that, you need to restart Spyder or the kernel associated with the console you want this change to take effect on.

A jupyter notebook would probably use the inline backend by default which cannot be animated (since it produces png images).
You may use the notebook backend to have the plot produced in an interactive notebook cell,
%matplotlib notebook
or the Tk backend to get a new window with the animation popping up,
%matplotlib tk
If using matplotlib 2.1, you may also use the %matplotlib inline backend and show the animation as JavaScript,
from IPython.display import HTML
HTML(ani.to_jshtml())

Related

Matplotlib notebook magic in Jupyter notebook partial plot

The first command in my Jupyter notebook is the %matplotlib notebook. This has been working fine but curiously the figures being drawn today are only partial plots. (The inline option without interactivity display complete plots).
When I pan, I can see more sections of the image.
I would like to see the entire plot and I am not sure what has caused this change in display
When image is first displayed it appears like this:
Image created upon running the cell
After panning it appears like this:
New section of image displayed upon panning
NOTE: This started to occur after updating to Matplotlib version 3.3.0.
For the current time I have reverted to Matplotlib version 3.1.0. But if the latest has some dependencies that I am missing which is causing the issue, I would like to learn how to figure those out.
I do not receive any errors or warnings upon import matplotlib or pyplot when working with 3.3.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 inline versus %matplotlib notebook display problems in pythonAnywhere jupyter

This is not a duplicate as suggested. I am not trying to switch backends. I am just trying to use the %matplotlib notebook formulation. Moreover, the solutions suggested in that other question (place the%matplotlib notebook before the the from matplotlib import pylplot as plt OR trying call the magic command twice in a row) do not work for me. The behavior (blank notebook charts) remains.
place the%matplotlib notebook before the the from matplotlib import pylplot as plt OR trying call the magic command twice in a row
When I use %matplotlib inline my charts always show, but they're generally on the small side, and have no ability to zoom/pan.
When I use %matplotlib notebook I get zoomable, pannable charts, but I sometimes encounter problems where the charts display as blank.
Background:
PythonAnywhere notebook 3.6 Python. Using update Google Chrome from Windows 7.
If I reopen my existing notebook, even restart the kernel and re-run all the cells, all my .plot() appear blank. But if I contemporaneously start a brand new 3.6 notebook, %matplotlib notebook plots work fine. Killing all the jupyter-related processes doesn't help either.
This does not always occur. But if one chart appears blank, they all appear blank.
I would like to use the notebook magic, but this problem is pretty profound.
I have found some stability on using %matplotlib notebook, after including plt.close(), before all my plots.
A pretty dirty solution, I know, but it's working. Here Windows 10, Python 3.6.2 and Jupyter 6.03 (updated Jupyter haven't solved the issue).
Hope this helps.
%matplotlib qt will do your job.

%matplotlib inline doesn't work on iPython and Jupyter console

I'm trying out Jupyter console for the first time, but can't get the %matplotlib inline magic to work. Below is a screenshot of an example session:
The plot shows in a separate window after I run Line 6, and Line 7 doesn't do anything.
When I run %matplotlib --list, inline is given as one of the options:
Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt',
'nbagg', 'agg', 'gtk', 'tk', 'ipympl', 'inline']
When I try to use another backend, say qt5, it gives an error message because I don't have any Qt installed.
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide
package to be installed, but it was not found.
Running %matplotlib?? reads:
If you are using the inline matplotlib backend in the IPython Notebook
you can set which figure formats are enabled using the following::
In [1]: from IPython.display import set_matplotlib_formats
In [2]: set_matplotlib_formats('pdf', 'svg')
The default for inline figures sets `bbox_inches` to 'tight'. This can
cause discrepancies between the displayed image and the identical
image created using `savefig`. This behavior can be disabled using the
`%config` magic::
In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
But I don't know if it's something I can tweak around to solve my issue.
When I try it the magic IPython console, it says inline is an Unknown Backend.
UnknownBackend: No event loop integration for u'inline'. Supported event loops are: qt,
qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
I've also found this issue on github after some googling but I don't even know if it's relevant to my situation (most of their conversation didn't make sense to me lol).
Lastly, I'm not sure if this issue is related at all, but here it is, just in case: when I try to open Vim in Jupyter via the !vim command, it glitches pretty badly, preventing me from even exiting out of Jupyter itself without closing the terminal altogther. Vim works perfectly fine when called inside IPython console, however.
I'm using matplotlib 2.0.0.
If anyone could help me figure this out, that'd be great! Thank you!
You’re running a console which is completely text based and incapable of showing images. Therefore, although inline is available, it's not producing inline output.
I'm not sure why it doesn't throw an error, though, which it does in my case:
You can use %matplotlib inline in a GUI console, like Jupyter QTConsole
or in a jupyter notebook in the browser
%matplotlib without the inline works for me (I'm using osx and IPython 7.0.1)
If you're running an old version of ipython try %pylab inline instead. See notes in this tutorial

Bokeh + interactive widgets + PythonAnywhere

I was unable to find a minimal working example for an interactive web app using bokeh and bokeh widgets that runs on PythonAnywhere.
Ideally, I would like to have a simple plot of a relatively complicated function (which I do not know analytically, but I have SymPy compute that for me) which should be replotted when a parameter changes.
All code that I have found so far does not do that, e.g. https://github.com/bokeh/bokeh/tree/master/examples, or refers to obsolete versions of bokeh.
Most of the documentation deals with running a bokeh-server, but there is no indication on how to have this running with WSGI (which is how PythonAnywhere handles the requests). For this reasone I have tried embedding a Bokeh plot within a Flask app. However, as far as I understand, in order to have interactive Bokeh widgets (which should trigger some computation in Python) do require a bokeh-server. I am not particularly attached to using either Flask or Bokeh, if I can achive a similar result with some other simpler tools. Unfortunately, a Jupyter notebook with interactive widgets does not seems to be an option in PythonAnywhere.
I have installed bokeh 0.12 on Python 3.5.
I have managed to run a simple bokeh plot within a flask app, but I am unable to use the Bokeh widgets.
Here is a working example of Jupyter notebook with interactive widgets on pythonanywhere:
%pylab inline
import matplotlib.pyplot as plt
from ipywidgets import interact
def plot_power_function(k):
xs = range(50)
dynamic_ys = [x ** k for x in xs]
plt.plot(xs, dynamic_ys)
interact(plot_power_function, k=[1, 5, 0.5])
PythonAnywhere does have the ipywidgets module pre-installed. But if you are not seeing the interactive widgets, make sure that you have run jupyter nbextension enable --py widgetsnbextension from a bash console to get it enabled for your notebooks. You will have to restart the jupyter server after enabling this extension (by killing the relevant jupyter processes from the consoles running processes list on the pythonanywhere dashboard).
As of Bokeh 0.12.5 you can embed Bokeh server applications directly in Jupyter notebooks. This is the best and most robust way to have interactive Bokeh plots and widgets (backed by real python code) in a notebook.
You can study an example of this in this demo notebook:
https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb
A screencast of that notebook in action is below:

Categories