【问题标题】:Scatter plot with a slider in pythonpython中带有滑块的散点图
【发布时间】:2016-11-17 01:30:57
【问题描述】:

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

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 以及放在那里以使其正常工作的内容。

提前致谢。

【问题讨论】:

    标签: python plot slider interactive scatter


    【解决方案1】:

    您需要改用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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      相关资源
      最近更新 更多