New plot generated when plot is created within function.
See original GitHub issueDear All, I’m trying to update a single plot (or re-generate the same plot) depending on a value given by an ipywidget, but I get new plots when the ggplot call is inside a function call. In my particular case, my “ggplot” call is inside a function called by an “observe” method attached to a widget. When the following example code is ran within a jupyter notebook, new plots are generated and appended to the notebook output every time the widget changes values:
from plotnine import *
from plotnine.data import *
from ipywidgets import widgets
from IPython.display import display
%matplotlib notebook
playSlider = widgets.IntSlider(min=0, max=5)
def createPlot(*args):
p = ggplot(mtcars, aes(x='hp', y='wt', color='mpg')) + geom_point() +facet_wrap("~cyl") + theme_bw()
p.draw()
playSlider.observe(createPlot, 'value')
display(playSlider)
Using %matplotlib notebook
or %matplotlib inline
makes no difference, new plot are appended to the cell’s output.
I did find the “_draw_using_figure” and “_create_figure” methods in the code, but my attempts to force new plots to be created in the same matplotlib figure were unsuccessful.
Is this a problem in the implementation or just me missing the correct syntax?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
I would be glad to help. I am a heavy user of ggplot in R, and your implementation for python made my day. I am switching my notebooks to use it, but as of now I have a lot of one-liners and notebook cells in R just to do ggplots. I can’t stand matplotlib…
Would you @has2k1 be interested in a notebook using this example, and maybe a couple other widgets? I can make a pull request for the plotnine-examples folder.
@melomcr, thanks for posting the solution. I am yet to make sense of it, but I think some form of this need to go into the documentation. Maybe even make it easier by providing the wrapper code if possible.