Giter VIP home page Giter VIP logo

Comments (10)

tiagopereira avatar tiagopereira commented on August 22, 2024

On a second look, this seems to happen because this small example (and nearly all in the ipywidgets examples) are just calling a new plot all the time. Catching the objects and using methods like ax.set_ydata seems to get it working, and is probably better code.

from ipympl.

vilim avatar vilim commented on August 22, 2024

My method for this is to generate the figure and axis objects outside interact, and then clearing the axes and redrawing the plot. It is slightly more convenient then generating the line objects first and then updating them. The interactive mode has to be activated for this to work (plt.ion()), in the previous iteration (%matplotlib notebook), it seems the interactive mode was enabled by default.

Maybe the example notebook could contain the canonical method for plotting with interact?

import ipympl
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact

plt.ion()

fig, ax = plt.subplots()
fig.show()
@interact
def plot_me(x:(0.1, 5)):
    ax.clear()
    ax.plot(np.sin(np.linspace(0, x)))

from ipympl.

jasongrout avatar jasongrout commented on August 22, 2024

Great idea to include an example. The interact decorator is really designed for output that is regenerated every time controls change, so it's awkward to use it with ipympl. ipympl works better if it is shown once and then updated when controls change, which is a different approach.

I think it would be great to have an easier way to set up a set of controls and trigger a side effect like updating an ipympl plot, sort of like interactive, without having an associated output widget.

from ipympl.

RemiLehe avatar RemiLehe commented on August 22, 2024

Not sure if this is related, but I recently tried the Lorentz-attactor example from here:
https://github.com/jupyter-widgets/ipywidgets/blob/master/docs/source/examples/Lorenz%20Differential%20Equations.ipynb

This works fine in Jupyter notebook (i.e. shows both the slider widgets and the plot).

However the slider widgets do not show up in Jupyter lab (this happens both with %matplotlib inline and %matplotlib ipympl):
screen shot 2018-03-07 at 8 14 51 am

Is there a recommend fix for this issue?

from ipympl.

SylvainCorlay avatar SylvainCorlay commented on August 22, 2024

@RemiLehe, this issue with Jupyterlab should be fixed if you update the @jupyter-widgets/jupyterlab-manager extension.

You probably have a version from before the beta?

from ipympl.

RemiLehe avatar RemiLehe commented on August 22, 2024

Thanks for the prompt answer!
I now see that I had ipywidgets 7.1.0 instead of ipywidgets 7.1.2. Upgrading solved my issue.

Thanks for your help!

from ipympl.

astrojuanlu avatar astrojuanlu commented on August 22, 2024

Edit: Confusion, see below.


I tried the original code without the %matplotlib xxx and plt.show():

import matplotlib.pyplot as plt
from ipywidgets import interact

def f(n):
    plt.plot([0,1,2],[0,1,n])

interact(f,n=(0,10));

And it almost works perfectly: first the plot is not shown, but the first update shows it and from there it's OK. I'm using latest JupyterLab and npm extensions.

In general, I feel that it's difficult to know when do we need %matplotlib inline or nothing, because in recent Jupyter versions I've noticed it's not necessary, but I cannot find the corresponding change in notebook, ipython or matplotlib.

Edit: My confusion is only related to inline, I understood that ipympl is the new interactive backend

from ipympl.

thomasaarholt avatar thomasaarholt commented on August 22, 2024

@Juanlu001 I do not get a plot without invoking the magic commands (%matplotlib ipympl / inline). Are you certain that you get this, especially if you restart the kernel first?

from ipympl.

astrojuanlu avatar astrojuanlu commented on August 22, 2024

@thomasaarholt you're right, I double checked and one still needs either a %matplotlib inline/ipympl or a plt.show(). I'm editing my comment, sorry for the confusion.

from ipympl.

ianhi avatar ianhi commented on August 22, 2024

I think it would be great to have an easier way to set up a set of controls and trigger a side effect like updating an ipympl plot, sort of like interactive, without having an associated output widget.

I implemented just this in https://github.com/ianhi/mpl-interactions. It has a function interactive_plot that uses set_data under the hood and otherwise behaves very similarly to the ipywidgets interactive function. (other difference is that tuples are passed to linspace rather than arange)

I had also proposed incorporating that function into ipympl in #251 but currently have settled on keeping in a separate library so I can more easily change(accidentally break...) things. It's on pypi as well now for convenience.

p.s. I feel deeply weird posting the above as it feels somewhat self promotional. But I'm trying to get over that feeling so that I can effectively share the solution I found to the problem we are all suffering. In that spirit does it it make sense to include an example using mpl-interactions in the ipympl examples? Also, if anyone tries it out I'd love to hear your feedback on how to improve it.

from ipympl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.