Comments (10)
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.
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.
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.
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
):
Is there a recommend fix for this issue?
from ipympl.
@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.
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.
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.
@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.
@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.
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)
- Latest version of everything still causes the dreaded Error displaying widget: model not found HOT 11
- An error in use of inset_axes of mpl_toolkits in Jupyter Notebook HOT 2
- [email protected]
- Comptability with mpl cursors
- Failed to load model class 'MPLCanvasModel' from module 'jupyter-matplotlib' Error: No version of module jupyter-matplotlib is registered. HOT 6
- Please remove jupyterlab==3.* requirement so as to allow 4.x to work. HOT 3
- Ability to configure defaults for `Canvas`
- Interactive graph - Saving
- Error: No version of module jupyter-matplotlib is registered HOT 5
- [test-bot] Scheduled Tests are failing
- Matplotlib Fails to Update Axis Limits with ipywidgets in Jupyter Lab HOT 1
- Question: how to use ipympl to simply include axes in ipywidets layout? HOT 1
- Large top/bottom padding for imshow images HOT 5
- Blank figures in Jupyter notebook HOT 13
- Cannot install with pip HOT 2
- %matplotlib widget run time error HOT 5
- Installing ipympl without matplotlib ? HOT 1
- MATLAB-like Vertical/Horizontal Zoom for strictly Vertical/Horizontal Drag HOT 1
- Click handler is broken with matplotlib>=3.5 HOT 1
- Conda Installation Safety Error - Size does not match reported size
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ipympl.