ipympl
ipympl copied to clipboard
ipympl for matplotlib not created through pyplot
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
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.
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
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 ..
And yet, if we do
from ipympl.backend_nbagg import Canvas
Canvas(fig)
nothing appears
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.
Should work well with 2D plots.
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.