-
Notifications
You must be signed in to change notification settings - Fork 84
Using Jupyter and Colab
WORK IN PROGRESS
There are several ways to access Jupyter, all slightly different. If you use notebook, or if you use Visual Studio Code you could use the Microsoft Jupyter extension.
At the top of the file you need to use a %matplotlib
magic command with one of the following options: inline
, notebook
, widget
.
Which options can be used in which environments, and their features, are summarised below:
%matplotlib |
Jupyter | Visual Code | Colab | animation | 3D rotation/zoom |
---|---|---|---|---|---|
inline |
✔️ | ✔️ | ✔️ | ||
notebook |
✔️ | ✔️ | ✔️ | ✔️ | |
widget |
✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
%matplotlib inline
import matplotlib.pyplot as plt
from spatialmath.base import *
trplot(transl(1,2,3))
trplot(transl(2,3,4))
Multiple plots within a cell are added to the figure. The resulting graphic in the notebook is a static PNG image, which cannot be rotated or zoomed.
There is an implicit plt.show()
at the end of the cell. The plot is rendered and displayed at that point. The plot is immutable and cannot be changed. Even if you saved references to the figure number or Axes
it cannot be changed.
%matplotlib notebook
import matplotlib.pyplot as plt
from spatialmath.base import *
trplot(transl(1,2,3))
trplot(transl(2,3,4))
Multiple plots within a cell are added to the figure. The resulting graphic can be rotated or zoomed using the mouse.
While the figure is active or on (the power switch icon), any figures created in subsequent cells are added to that figure. If the figure is turned off, then new figures will be added to a new figure which starts in the on state.
This mode is not supported for Visual Studio.
%matplotlib widget
widget
is a synonym for ipympl
and the package ipympl
must be installed. This backend provides much smoother and more performant axes rotation and zooming than notebook
. The ipympl
package also supports a rich set of user interface components like sliders and radio buttons.
The programming model is quite different to inline
in that figures are mutable and can be updated in later cells. This means that if you want a new figure it must be created explicitly using something like plt.figure()
If the code to be executed is
tranimate(transl(1,2,3))
then, except for the inline
case, it will be animated inside the notebook figure and the axes can be rotated and zoomed manually at the same time. The animation runs just once, and the cell has to be re-executed to re-run the animation.
For the inline
case we can render the animation to an HTML5/Javascript fragment
html = tranimate(transl(1,2,3), movie=True)
from IPython.core.display import HTML
HTML(html)
and then display it.
To see all the options use
%matplotlib --list
Switching between graphical modes should depend only the %matplotlib
magic command but in practice switching is not so reliable. Occasionally the Jupyter kernel may need to be restarted.
A cell can create multiple figures, and you can use figure and axis handles to control which one is drawn into. Once the cell has stopped executing the figures are closed off, and can no longer be added to.
The inline
and widget
modes work as described above. For widget
you must first do this
!pip install ipympl
%matplotlib widget
from google.colab import output
output.enable_custom_widget_manager()