question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New plot generated when plot is created within function.

See original GitHub issue

Dear 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:open
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
melomcrcommented, Mar 9, 2018

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.

1reaction
has2k1commented, Mar 9, 2018

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating custom plotting functions with matplotlib
Here, I will show you how to create your own custom plotting functions that can be easily used by calling them within your...
Read more >
How to plot inside a function by generating a counter
How can one generate a counter within the function, to provide the x-axis values? import matplotlib.pyplot as plt import numpy as np from...
Read more >
How to plot a function defined with def in Python? (Matplotlib)
Create x data points using numpy. Plot x and f(x) using plot() method. To display the figure, use show() method. Example.
Read more >
Plot a function in Python with Matplotlib | EasyTweaks.com
In this tutorial we will show how to you can easily plot a function with Python and specifically using the Numpy, Matplotlib and...
Read more >
How To Display A Plot In Python using Matplotlib - ActiveState
The matplotlib.pyplot.plot() function provides a unified interface for creating different types of plots. The simplest example uses the plot() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found