简体   繁体   English

Matplotlib 图未使用 ipywidgets slider 更新

[英]Matplotlib figure is not updating with ipywidgets slider

I have the following code to generate a simple graph.我有以下代码来生成一个简单的图表。

%matplotlib notebook
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
line, = ax.plot(x, np.sin(x))

def update(w = 1.0):
    line.set_ydata(np.sin(w * x))
    plt.show()

interact(update)

The code generates the plot just fine - output代码生成 plot 就好了 - output

But when I drag the slider, the figure will not update.但是当我拖动slider时,图形不会更新。 Any ideas on why this is?关于为什么会这样的任何想法?

Note: Your code actually works for me out of the box, so it may be worth updating your dependencies and see if that fixes it.注意:您的代码实际上对我来说是开箱即用的,因此可能值得更新您的依赖项并查看是否可以修复它。

However, the main thing you want to change is to call fig.canvas.draw() instead of plt.show()但是,您要更改的主要内容是调用fig.canvas.draw()而不是plt.show()

%matplotlib notebook
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
line, = ax.plot(x, np.sin(x))

def update(w = 1.0):
    line.set_ydata(np.sin(w * x))
    fig.canvas.draw()

interact(update)

Using Ipympl使用 Ipympl

There is also a widget based notebook backend (that will also work in jupyterlab): ipympl which you can install with pip install ipympl and use with %matplotlib ipympl还有一个基于小部件的笔记本后端(也可以在 jupyterlab 中使用): ipympl您可以使用pip install ipympl并与%matplotlib ipympl使用

In general the ipympl backend will work better with other widgets than the notebook backend.一般来说,与笔记本后端相比,ipympl 后端可以更好地与其他小部件一起使用。

using interactive with matplotlib使用与 matplotlib interactive

One unfortunate consequence of interactive is that it assumes the output will be fully regenerated every time the slider value changes. interactive的一个不幸后果是它假定 output 将在每次 slider 值更改时完全重新生成。 This doesn't always play super nicely with the set_data methods you are using.这并不总是与您使用的set_data方法配合得很好。 So you are likely better off manually generating and connecting the sliders.因此,您最好手动生成和连接滑块。 I'll also note that I've written a package that automates using the set_data command to connect widgets to updating matplotlib plots: https://mpl-interactions.readthedocs.io/en/stable/ .我还要注意,我编写了一个 package,它使用set_data命令自动连接小部件以更新 matplotlib 图: Z5E056C500A1C4B6A7110B50D807BADE-5Z//mplstables ./en。 With that package your code would be使用 package 您的代码将是

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import mpl_interactions.ipyplot as iplt

x = np.linspace(0, 2 * np.pi, 1000)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

def f(x, w):
    return np.sin(w * x)

controls = iplt.plot(x, f, w=(1, 10))

This would do a job.这会做的工作。

# load the interactive tool
from ipywidgets import interact, interactive, widgets, fixed
try:
    from ipywidgets import Layout
except:
    pass 

import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook

def fta(freq = 50.0):
    "showing sine frequency"
    y = np.sin(freq*x)
    f, ax1 = plt.subplots(nrows=1,figsize=(8,6))
    ax1.plot(x[0:100], y[0:100],'b')
    ax1.set_ylim(ymin=-1.1, ymax=1.1)
    ax1.grid();
    # then use it interactively,
interactive( fta, freq=(0.0,100.0))

互动情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM