更新 pyplot.scatter 的位置和颜色 [英] Updating the positions and colors of pyplot.scatter
问题描述
我已经为此苦苦挣扎了一段时间,但无法让它发挥作用.我正在读取文件,并从中散点数据,我想通过更新 for
循环中每个块的散点图来动画化"该文件(并使它适应实时变化)数据流).
I have been struggling with this for a while and can't get it to work. I am reading a file in chunks and scatter plotting data from it, and I would like to "animate" it by updating the scatter plot for each chunk in a for
loop (and also adapt it to a live stream of data).
因此,类似此丑陋示例的示例适用于单个图:
So something like this ugly example works for a single plot:
x = [1, 2, 3, 4]
y = [4, 3, 2, 1]
alpha = [0.2, 0.3, 0.8, 1.0]
c = np.asarray([(0, 0, 1, a) for a in alpha])
s = scatter(x, y, marker='o', color=c, edgecolors=c)
但是如何在不重复调用 s.remove()
和 scatter()
的情况下更新绘图?完全不直观的 s.set_array
和 s.set_offsets
可以更新颜色以及x和y位置,但是我不知道如何使用它们上面我有x,y,alpha数据的类型.
But how do I update the plot without calling s.remove()
and scatter()
repeatedly? The completely unintuitively-named s.set_array
and s.set_offsets
are supposed to update the colors and the x and y positions, but I can't figure out how to use them with the type of x, y, alpha data I have above.
(还有,在上面的图中有更好的方法做alpha吗?)
(Also, is there a better way to do the alpha in the above plot?)
推荐答案
我为此找到的解决方案包括使用 Normalize 根据相关数据制作标准化颜色列表,将其映射到 ScalarMappable,并使用它来设置动画每一帧的面部颜色和c限制.使用 scat 散点图的句柄和 speedList 提供颜色数据:
The solution I found for this involves using Normalize to make a normalised colour list based on the relevant data, mapping it to a ScalarMappable, and using that to set the face colour and c limits on each frame of the animation. With scat the handle of the scatter plot and speedsList provides the colour data:
n = mpl.colors.Normalize(vmin = min(speedsList), vmax = max(speedsList))
m = mpl.cm.ScalarMappable(norm=n, cmap=mpl.cm.afmhot)
scat.set_facecolor(m.to_rgba(speedsList))
scat.set_clim(vmin=min(speedsList), vmax=max(speedsList))
这正是我所期望的.
这篇关于更新 pyplot.scatter 的位置和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!