python中带滑块的散点图

python中带滑块的散点图,python,plot,slider,interactive,scatter,Python,Plot,Slider,Interactive,Scatter,嘿,我正在尝试创建一个带有滑块的散点图,当我滑动时,滑块会更新该图。这是到目前为止我的代码。它画了一个散点图和一个滑块,但当我移动它时,什么也没发生。我怀疑问题出在.set_ydata位上,但我似乎无法在互联网上找到其他方法 import numpy as np from matplotlib.widgets import Slider from pylab import plot, show, figure, scatter, axes, draw fig = figure() ax =

嘿,我正在尝试创建一个带有滑块的散点图,当我滑动时,滑块会更新该图。这是到目前为止我的代码。它画了一个散点图和一个滑块,但当我移动它时,什么也没发生。我怀疑问题出在
.set_ydata
位上,但我似乎无法在互联网上找到其他方法

import numpy as np 
from matplotlib.widgets import Slider
from pylab import plot, show, figure, scatter, axes, draw

fig = figure()
ax = fig.add_subplot(111)

x, y = np.random.rand(2,100)
scat = scatter(x,y)

axcolor = 'lightgoldenrodyellow'
axamp = axes([0.2, 0.01, 0.65, 0.03], axisbg=axcolor)

scorr = Slider(axamp, 'corr', -2,2, valinit=1)

def update(val):
    corr = scorr.val

    for i in range(len(x)):
        x[i]=x[i]+corr*np.random.randn()

    for i in range(len(y)):
        y[i]=y[i]+corr*np.random.randn()

    scat.set_xdata(x)
    scat.set_ydata(y)
    draw()

scorr.on_changed(update)

show(scat)
公平地说,这只是一个测试脚本。我必须用更复杂的脚本做同样的事情,但我意识到在一个更简单的问题上尝试它会更容易。我真的只关心scat.setydata以及放在那里的内容,这样它就可以工作了


提前感谢。

您需要使用
set\u offset
set\u array
代替:

# make sure you get the right dimensions and direction of arrays here
xx = np.vstack ((x, y))
scat.set_offsets (xx.T)

# set colors
scat.set_array (y)
可能与以下内容重复: