嘿,我正在尝试创建一个带有 slider 的散点图,当我滑动时该 slider 会更新该图。这是我到目前为止的代码。它绘制了一个散点图和一个 slider ,但当我移动它时,什么也没有发生。我怀疑问题出在 .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.set_ydata
以及放在那里的内容才能使其正常工作。
提前致谢。
最佳答案
您需要使用 set_offsets
和 set_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)
关于python - python 中带有 slider 的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38368990/