johannesjmeyer / rsmf

Right-size my figures
174 stars 9 forks source link

No display in Jupyter notebooks #3

Open quantshah opened 4 years ago

quantshah commented 4 years ago

I suppose this was not the original intention of the package - to support plotting inside a notebook. But if I do this in a Jupyter notebook, I cannot see any plots:

formatter = rsmf.setup(r"\documentclass[a4paper,12pt,noarxiv]{quantumarticle}")
fig = formatter.figure(aspect_ratio=1)
ax = fig.add_subplot(111)
ax.imshow(np.random.uniform(size=(5,5)))
plt.show()

However there are no issues if I save the plot, even in a notebook.

johannesjmeyer commented 4 years ago

Yes, this is an issue with the PGF backend in general. Actually, rsmf is based on a notebook I made to achieve the same functionality for Quantum alone. You find it here. In certain versions of Jupyter notebook it seems to work to have both PDF output with PGF and a display of the figure, but this involves some serious hackery as you can see in the notebook (i.e. using magic commands and switching between backends). And it's not even reproducible.

I guess the most reasonable way to do it would be to load the PDF file that is created into the notebook again, as proposed here. One could make an api for that like

rsmf.save_and_show("output.pdf")

What do you think @quantshah ?

johannesjmeyer commented 4 years ago

I think the best would be to dynamically detect if one can import IPython and then use the approach detailed here.

quantshah commented 4 years ago

If I use your code in the way I proposed in #4 things work out of the box in Jupyter notebooks.

quantshah commented 4 years ago

Yes, this is an issue with the PGF backend in general. Actually, rsmf is based on a notebook I made to achieve the same functionality for Quantum alone. You find it here. In certain versions of Jupyter notebook it seems to work to have both PDF output with PGF and a display of the figure, but this involves some serious hackery as you can see in the notebook (i.e. using magic commands and switching between backends). And it's not even reproducible.

I guess the most reasonable way to do it would be to load the PDF file that is created into the notebook again, as proposed here. One could make an api for that like

rsmf.save_and_show("output.pdf")

What do you think @quantshah ?

This looks interesting but I feel like the more we keep the rsmf code in the background and use matplotlib defaults, the better. So I could make the plots show on notebooks directly and save them like regular matplotlib figures using just the initialisation at the top.

johannesjmeyer commented 4 years ago

Now I think the best way forward is to just wrap the IPython IFrame explained here as rsmf.show("xyz.pdf").

johannesjmeyer commented 4 years ago

Okay, this doesn't work as expected, see PR #14