seismo-live / seismo_live

Live Jupyter Notebooks for Seismology
http://seismo-live.org
78 stars 74 forks source link

Animations broken in some notebooks #22

Closed megies closed 6 years ago

megies commented 6 years ago

As @heinerigel encountered, some animations in some notebooks are broken when using very recent Anaconda (due to changes in Jupyter and/or Matplotlib, likely).

For example, try https://github.com/krischer/seismo_live/blob/master/notebooks/Computational%20Seismology/The%20Finite-Difference%20Method/fd_elastic1d_staggered_solution.ipynb on current seismo-live server. Animation does not show and is stuck on first plot.

It looks like using the convenience function plt.switch_backend('nbagg') isn't doing the job anymore and the problem is solved by doing it the "old" canonical/failsafe way, by using matplotlib.use('nbagg') as the first command after a plain import matplotlib at top of the code when no backend is yet selected by matplotlib, to avoid problems with backend switching.

If you take the first cell of that notebook, which currently loos like this and doesn't work:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['figure.facecolor'] = 'w'          # remove grey background

# Show the plots in the Notebook.
plt.switch_backend("nbagg")

Changing it like this works:

import numpy as np
import matplotlib
matplotlib.use("nbagg")
import matplotlib.pyplot as plt
matplotlib.rcParams['figure.facecolor'] = 'w'          # remove grey background

Using IPython "magic" command also works:

%matplotlib notebook

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['figure.facecolor'] = 'w'          # remove grey background

It's mostly a matter of taste which one is used. But the matplotlib.use() option has the pro that if downloading the notebook as plain Python code, the backend can be changed to tkagg in the matplotlib.use() call and then the code also runs when executing the Python program outside of Jupyter.

CC @heinerigel @krischer

megies commented 6 years ago

@krischer Another way to approach this might be to put a matplotlibrc file on the server, so that nbagg backend is used all across seismo-live globally (would also allow for setting proper default figure size etc.). Only drawback might be that then when users download the notebooks, they might behave a bit differently locally..

krischer commented 6 years ago

I actually just realized that the agg instead of the nbagg backend was forced in the Dockerfile...fixed in https://github.com/krischer/seismo_live/commit/8a3b68dcf9c82e8e82f051b7ce1eb8efd2965da9. Should be live at the next restart of seismo-live.