%matplotlib inline

Thongchan Thananate
2 min readApr 9, 2022

--

why?

captured by author

someone asks why there is `%matplotlib inline` in someone’s notebook
the answer is, well, quite shortly explained

If you use Python kernel below version 3.7, the plot will not show in notebook.
For Python 3.7+ that comes with Conda package or eagerly updated Data Wranglers, it’s fine to remove %matplotlib inline

you can easily check it’s already inline in the backend by using simple function
print(matplotlib.get_backend())
or
print(plt.get_backend())

It will return module://matplotlib_inline.backend_inline showing that it’s already inline

However, if you’re still curious about this magic. Here is your explanations.

IPython has a set of predefined ‘magic functions’ that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.

With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.

or their document here.

Ready, set, Py!

--

--

Thongchan Thananate

People might laugh at it or call it foolish logic, but that’s enough for me. That’s what romanticism is about!