ipympl icon indicating copy to clipboard operation
ipympl copied to clipboard

ipympl for matplotlib not created through pyplot

Open nthiery opened this issue 4 years ago • 7 comments

Is it possible to use ipympl starting from an arbitrary matplotlib figure? If yes, an example would be helpful. Thanks!

Use Case: I would like to use ipympl for SageMath plots. The later do have a method to convert them to matplotlib figure. However the figure has not been created with the usual pyplot workflow, and does not have a canvas setup.

In a jupyter notebook with the SageMath kernel (e.g. using the binder link from https://github.com/sagemath/sage-binder-env/ after installing ipympl with sage -pip install ipyml)

%matplotlib widget
p = plot(sin(x))
fig = p.matplotlib();
type(fig.canvas)
<class 'NoneType'>

It could well be that SageMath is producing weird matplotlib figures though.

cc: @zerline

nthiery avatar May 09 '20 08:05 nthiery

This looks more like SageMath is producing nonstandard (?) matplotlib figures. Check the source code to see how matplotlib is initialised in SageMath. Particularly through the p.matplotlib() command.

thomasaarholt avatar May 18 '20 16:05 thomasaarholt

Thanks for the feedback. This could well be; and in fact be ideal for me as the solution would lie somewhere in the home field :-)

Nevertheless,it would be helpful to have an example of a widget produced from a matplotlib figure not produced with pyplot1

nthiery avatar May 18 '20 16:05 nthiery

We are not too far, though:

fig = plot(sin).matplotlib()
from ipympl.backend_nbagg import new_figure_manager_given_figure
new_figure_manager_given_figure(1,fig)

gives out expected result ..

zerline avatar Jun 01 '20 12:06 zerline

And yet, if we do

from ipympl.backend_nbagg import Canvas
Canvas(fig)

nothing appears

zerline avatar Jun 01 '20 13:06 zerline

We are not too far, though:

fig = plot(sin).matplotlib();
from ipympl.backend_nbagg import new_figure_manager_given_figure
new_figure_manager_given_figure(1,fig)

gives out expected result ..

Ah ah! Well done! I had tried something along these lines and failed.

So now the next step is to try it out on a variety of Sage plots to check the robustness.

nthiery avatar Jun 02 '20 07:06 nthiery

Should work well with 2D plots.

zerline avatar Jun 09 '20 12:06 zerline

from ipympl.backend_nbagg import new_figure_manager_given_figure new_figure_manager_given_figure(1,fig)

It works but doesn't look like a recommended way to do this, as new_figure_manager_given_figure is a method of protected class _Backend_ipympl. Should I call manager.destroy() manually? Purpose of the num argument is also unclear: can I use the same number for multiple figures?

I want to use a library that creates matplotlib figures with Figure() (that is the recommended way for web servers according to Matplotlib documentation), and then display them in Voila application with ipympl. It will be great if such use case is described in ipympl documentation.

egormkn avatar Sep 08 '22 10:09 egormkn