Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipympl for matplotlib not created through pyplot #221

Open
nthiery opened this issue May 9, 2020 · 7 comments
Open

ipympl for matplotlib not created through pyplot #221

nthiery opened this issue May 9, 2020 · 7 comments

Comments

@nthiery
Copy link

nthiery commented May 9, 2020

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

@thomasaarholt
Copy link
Contributor

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.

@nthiery
Copy link
Author

nthiery commented May 18, 2020

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

@zerline
Copy link

zerline commented Jun 1, 2020

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
Copy link

zerline commented Jun 1, 2020

And yet, if we do

from ipympl.backend_nbagg import Canvas
Canvas(fig)

nothing appears

@nthiery
Copy link
Author

nthiery commented Jun 2, 2020 via email

@zerline
Copy link

zerline commented Jun 9, 2020

Should work well with 2D plots.

@egormkn
Copy link

egormkn commented Sep 8, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants