Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render matplotlib plots in ipython

I have a container running with Python/JupyterLab and all of my dependencies. I start it with:

docker run --rm -it -p 8888:8888 \
  --mount type=bind,source=/project,target=/work \
  python-3.9.1-jupyterlab

It launches jupyterlab and I can connect through the browser. Everything is good.


Now I am experimenting with using VSCode as a Python IDE. It is helpful to attach a shell from VSCode to my container so I can run iPython and edit my code all in one place. I run "attach shell" from the VSCode Docker extension:

docker exec -it {containerID} bash <

Then I open an iPython shell:

jo@:~/work $ ipython --pylab

Python 3.9.1
IPython 7.20.0 -- An enhanced Interactive Python.
Using matplotlib backend: agg

In [1]: matplotlib.get_backend()
Out[1]: 'agg'

In [2]: import matplotlib.pyplot as plt

In [3]: plt.plot([1.6, 2.7])
Out[3]: [<matplotlib.lines.Line2D at 0x7f5ed0ed8d30>]

In [4]: plt.show()

In [5]: %matplotlib inline

In [6]: plt.plot([1.6, 2.7])
Out[6]: [<matplotlib.lines.Line2D at 0x7f5ed0df5d60>]
<Figure size 432x288 with 1 Axes>

In [7]: plt.show()

I cannot see any plots. I have tried rendering them with different backends (default was 'agg'). I am thinking this is because the kernel -- executing on the container -- cannot make use of the host graphics (i.e., kernel can render plots but cannot display them). Perhaps I do not have host/container ports mapped properly.

Could someone provide some guidance on things to try? Here is the image for the container I am using.

like image 458
Jeff Avatar asked Feb 27 '21 15:02

Jeff


People also ask

How do I view IPython plots?

IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.

How do I run matplotlib in Jupyter?

Install Matplotlib Make sure you first have Jupyter notebook installed, then we can add Matplotlib to our virtual environment. To do so, navigate to the command prompt and type pip install matplotlib. Now launch your Jupyter notebook by simply typing jupyter notebook at the command prompt.

How do you show plots in Jupyter notebook?

Usually, displaying plots involves using the show() function from PyPlot. With Jupyter notebooks, this isn't necessary as the plots are displayed after running the cells containing the code that generates them. These plots are by default, displayed inline, which means, they're displayed in the notebook itself.


1 Answers

You are running jupyterlab in a shell, which is not a graphical environment. In addition, if you are running the Docker container from non X11 desktop, the DISPLAY variable (used by X11 to display graphics) will not be set. If you are running it from a linux desktop, you can set the environment to allow the X11 as in this example, but it's not same for all environments and you might need to research further how to set your specific environment to allow X11 servers from docker container to communicate with X11 client on the host (note that in X11, the application is the server and the desktop is the client)

On Linux, if the environment variable DISPLAY is unset, the "event loop" is identified as "headless", which causes a fallback to a noninteractive backend (agg). You can read more about the backends and how they are selected in the matplotlib documents. Once the "even loop" is set to be "headless", all of the rendering will be done in files. You can check this answer on how to use the mahtplotlib with this backend.

A good approach to use Jupyther and the benefits of VSCode would be to use an extension for VSCode. One of the most popular extensions is jupyter developed by Microsoft. The extension will allow you to have the same experiance as using the browser.

like image 152
jordanvrtanoski Avatar answered Sep 28 '22 05:09

jordanvrtanoski