gpt4 book ai didi

python - 实时向 matlibplot 散点图添加点

转载 作者:太空狗 更新时间:2023-10-30 02:10:01 25 4
gpt4 key购买 nike

我想在 matplotlib 中将点“实时”添加到散点图,以便这些点在计算后立即出现在图表上。是否可以?如果没有,是否有可以完成此操作的 python 兼容的类似绘图平台?谢谢!

最佳答案

您可以将新点附加到ax.scatter 返回值的offsets 数组。

您需要使绘图与 plt.ion() 交互并使用 fig.canvas.update() 更新绘图。

这取自二维标准正态分布并将点添加到散点图:

import matplotlib.pyplot as plt
import numpy as np

plt.ion()

fig, ax = plt.subplots()

plot = ax.scatter([], [])
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)

while True:
# get two gaussian random numbers, mean=0, std=1, 2 numbers
point = np.random.normal(0, 1, 2)
# get the current points as numpy array with shape (N, 2)
array = plot.get_offsets()

# add the points to the plot
array = np.append(array, point)
plot.set_offsets(array)

# update x and ylim to show all points:
ax.set_xlim(array[:, 0].min() - 0.5, array[:,0].max() + 0.5)
ax.set_ylim(array[:, 1].min() - 0.5, array[:, 1].max() + 0.5)
# update the figure
fig.canvas.draw()

关于python - 实时向 matlibplot 散点图添加点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32651818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com