betatim / notebook-as-pdf

Save Jupyter Notebooks as PDF

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Image results are not shown in the PDF if `%matplotlib` magic commands are used

Cube707 opened this issue · comments

Hello there, loving your Project so far.
I just tried using it to export some explanations on how to use matplotlib and realized the following:

Problem:

When using magic comments like %matplotlib notebook or %matplotlib widget to produce interactive plot windows, the results are not displayed in the final PDF.
This is not a huge issue as you can comment out these commands and run the notebook again to get it into a state where the export works, but I feel this is an unnecessary step that could be avoided.

Example code:

Here is a example cell so you can test it yourselfe:

%matplotlib widget

from matplotlib import pyplot as plt
from math import sin

x = list(range(100))
y = [sin(x) for x in x]

plt.plot(x,y)

some analysis:

Looking at the actual json data, it seems like the image is just there in the same form in both cases. So from my perspective the same functionality could be used to display both image types

Json representation without magic:

"outputs": [
    {
        "output_type": "execute_result",
        "data": {
            "text/plain": [
                "[<matplotlib.lines.Line2D at 0x229ac10ba30>]"
            ]
        },
        "metadata": {},
        "execution_count": 1
    },
    {
        "output_type": "display_data",
        "data": {
            "image/png": "loads of base64...", //here is the actuall image 
            "text/plain": [
                "<Figure size 432x288 with 1 Axes>"
            ]
        },
        "metadata": {
            "needs_background": "light"
        }
    }
],

Json representation with magic:

"outputs": [
    {
        "output_type": "execute_result",
        "data": {
            "text/plain": [
                "[<matplotlib.lines.Line2D at 0x229aef84dc0>]"
            ]
        },
        "metadata": {},
        "execution_count": 2
    },
    {
        "output_type": "display_data",
        "data": {
            "application/vnd.jupyter.widget-view+json": {
                "version_major": 2,
                "version_minor": 0,
                "model_id": "c9dfc6ced3c74d28882947a564d6eb03"
            },
            "image/png": "loads of base64...", //more or less the same as above
            "text/plain": [
                "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
            ]
        },
        "metadata": {}
    }
],

seems to be an upstream problem with the html exporter. Should I close this here or do you want to keep it open?

closed upstream by jupyter/nbconvert#1664