Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter: how to make simple illustrations

I am learning to use Jupyter/IPython Notebook as an electronic notebook. Sometimes I need simple illustrations to go along with my calculations, e.g. arrows to represent vector quantities. That's the kind of illustration for which TikZ would be used if we were in Latex. Having tried the TikZ magic extension and failed, I wonder if there's a more native (Python) way to do this. I don't see Matplotlib as the right tool for this sort of thing (correct me if I'm wrong).

If you think TikZ magic is indeed the way to go and I should try to get it to work, then do say so. Thanks.

like image 406
bongbang Avatar asked Jun 02 '15 17:06

bongbang


People also ask

How to create an interactive plot in Jupyter Notebook?

Interactive Plot in Jupyter Notebook In order to create an interactive plot in Jupyter Notebook, you first need to enable interactive plot as follows: After that, we import the required libraries. Especially FuncAnimation class that can be used to create an animation for you. Next, we need to create an initial state of the animation figure.

How to insert an image in a Jupyter Notebook?

Step 1: This method is the easiest. first, change the type of the cell to -> markdown. Step 2: After that click edit in the jupyter notebook menu. after that click ‘insert image’. Edit -> insert image. Step 3: After that, a dialogue box opens up and asks us to locate the file.

What have we learned from Jupyter notebooks?

Starting from scratch, we have come to grips with the natural workflow of Jupyter Notebooks, delved into IPython’s more advanced features, and finally learned how to share our work with friends, colleagues, and the world. And we accomplished all this from a notebook itself!

What is Jupyter Voila and how do I use it?

If you work with Jupyter notebooks you most probably heard of “Voila”. What it does is that it takes a regular Jupyter notebook, keeps the output cells, and hides the code cells. This results in a much cleaner look. Voila is super-easy to install. If not already installed in your environment, you can install it by:


1 Answers

TikZ (prefered solution)

If you're already familiar with TikZ the respective magic is probably the best option. To use it, simply follow the installation instruction in this repo (pip install git+git://github.com/mkrphys/ipython-tikzmagic.git) and load the extension as shown in on the githib page with %load_ext tikzmagic.
I just tried with IPython 3.1 and it works fine. Of course you have to have pdflatex available.

Matplotlib

If you want to draw simple arrows matplotlib can be used as well and is, of course, more pythonic than TikZ. A really simple example based on this example could look like

import matplotlib.pyplot as plt
%matplotlib inline
plt.axis('off')
plt.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, fc='k', ec='k');

For more technical plots with lots of arrows and dimensions, I totally agree with you that matplotlib is not be preferred.

Other alternatives

There is also an asymptote magic found here. I haven't tried this yet, though.

Finally, you could use svgs either written in the notebook (hints see this question, or using Inkscape or similar and embed the resulting svg-file via the from IPython.display import SVG.

like image 188
Jakob Avatar answered Sep 18 '22 22:09

Jakob