python - python 中带有 slider 的散点图

标签 python plot slider interactive scatter

嘿,我正在尝试创建一个带有 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_offsetsset_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)

可能重复:How to animate a scatter plot?

关于python - python 中带有 slider 的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38368990/

相关文章:

python - 使用 NLTK 提取关系

python - 在 DynamoDB 中使用带有保留字的 ProjectionExpression 和 Boto3

python - 是否可以在不同线程上为不同图像运行 knnMatch?

R Plotly - 绘制多条回归线

javascript - 连接范围 slider Jquery

python - 属性错误: module 'dill._dill' has no attribute 'log'

python - matplotlib 特定数量的网格点的流图错误

plot - gnuplot 为标签指定列

javascript - jQuery如何加载图像的一部分

javascript - html-css 自动 slider 按钮不起作用