简体   繁体   English

Python:Matplotlib 实时更新散点图

[英]Python:Matplotlib update scatter graph in real time

I am creating a projectile motion simulator, using the scatter function in matplotlib.我正在创建一个弹丸运动模拟器,使用 matplotlib 中的 scatter 函数。 I have got a list of coordinates stored in an ordered dictionary but want to be able to add these coordinates one at a time with a given time interval.我有一个存储在有序字典中的坐标列表,但希望能够以给定的时间间隔一次添加一个坐标。

PSEUDOCODE/Bad Python:伪代码/坏 Python:

path = OrderedDict([(0.0707, 0.0702), (0.141, 0.139), (0.212, 0.208), (0.283, 0.275), (0.354, 0.341), (0.424, 0.407), (0.495, 0.471), (0.566, 0.534), (0.636, 0.597), (0.707, 0.658), (0.778, 0.719), (0.849, 0.778), (0.919, 0.836), (0.99, 0.894), (1.06, 0.95), (1.13, 1.01), (1.2, 1.06), (1.27, 1.11), (1.34, 1.17), (1.41, 1.22), (1.48, 1.27), (1.56, 1.32), (1.63, 1.37), (1.7, 1.41), (1.77, 1.46), (1.84, 1.51), (1.91, 1.55), (1.98, 1.6), (2.05, 1.64), (2.12, 1.68), (2.19, 1.72), (2.26, 1.76), (2.33, 1.8), (2.4, 1.84), (2.47, 1.87), (2.55, 1.91), (2.62, 1.95), (2.69, 1.98), (2.76, 2.01), (2.83, 2.04), (2.9, 2.08), (2.97, 2.11), (3.04, 2.13), (3.11, 2.16), (3.18, 2.19), (3.25, 2.22), (3.32, 2.24), (3.39, 2.27), (3.46, 2.29), (3.54, 2.31), (3.61, 2.33), (3.68, 2.35), (3.75, 2.37), (3.82, 2.39), (3.89, 2.41), (3.96, 2.42), (4.03, 2.44), (4.1, 2.45), (4.17, 2.47), (4.24, 2.48), (4.31, 2.49), (4.38, 2.5), (4.45, 2.51), (4.53, 2.52), (4.6, 2.53), (4.67, 2.53), (4.74, 2.54), (4.81, 2.54), (4.88, 2.55), (4.95, 2.55), (5.02, 2.55), (5.09, 2.55), (5.16, 2.55), (5.23, 2.55), (5.3, 2.55), (5.37, 2.54), (5.44, 2.54), (5.52, 2.53), (5.59, 2.53), (5.66, 2.52), (5.73, 2.51), (5.8, 2.5), (5.87, 2.49), (5.94, 2.48), (6.01, 2.47), (6.08, 2.46), (6.15, 2.44), (6.22, 2.43), (6.29, 2.41), (6.36, 2.39), (6.43, 2.38), (6.51, 2.36), (6.58, 2.34), (6.65, 2.32), (6.72, 2.3), (6.79, 2.27), (6.86, 2.25), (6.93, 2.22), (7.0, 2.2), (7.07, 2.17), (7.14, 2.14), (7.21, 2.11), (7.28, 2.08), (7.35, 2.05), (7.42, 2.02), (7.5, 1.99), (7.57, 1.96), (7.64, 1.92), (7.71, 1.89), (7.78, 1.85), (7.85, 1.81), (7.92, 1.77), (7.99, 1.73), (8.06, 1.69), (8.13, 1.65), (8.2, 1.61), (8.27, 1.57), (8.34, 1.52), (8.41, 1.48), (8.49, 1.43), (8.56, 1.38), (8.63, 1.33), (8.7, 1.28), (8.77, 1.23), (8.84, 1.18), (8.91, 1.13), (8.98, 1.08), (9.05, 1.02), (9.12, 0.968), (9.19, 0.911), (9.26, 0.854), (9.33, 0.796), (9.4, 0.737), (9.48, 0.677), (9.55, 0.616), (9.62, 0.554), (9.69, 0.491), (9.76, 0.427), (9.83, 0.361), (9.9, 0.295), (9.97, 0.229), (10.0, 0.161), (10.1, 0.0916), (10.2, 0.0217)])


for each in path:

    graph.update(each)

    time.sleep(0.1)

Any help would be much appreciated :)任何帮助将非常感激 :)

This works for me: (at least for prototyping)这对我有用:(至少对于原型设计)

import matplotlib.pyplot as plt

path = [(0.0707, 0.0702), (0.141, 0.139), (0.212, 0.208), (0.283, 0.275), (0.354, 0.341), (0.424, 0.407), (0.495, 0.471), (0.566, 0.534), (0.636, 0.597), (0.707, 0.658), (0.778, 0.719), (0.849, 0.778), (0.919, 0.836), (0.99, 0.894), (1.06, 0.95), (1.13, 1.01), (1.2, 1.06), (1.27, 1.11), (1.34, 1.17), (1.41, 1.22), (1.48, 1.27), (1.56, 1.32), (1.63, 1.37), (1.7, 1.41), (1.77, 1.46), (1.84, 1.51), (1.91, 1.55), (1.98, 1.6), (2.05, 1.64), (2.12, 1.68), (2.19, 1.72), (2.26, 1.76), (2.33, 1.8), (2.4, 1.84), (2.47, 1.87), (2.55, 1.91), (2.62, 1.95), (2.69, 1.98), (2.76, 2.01), (2.83, 2.04), (2.9, 2.08), (2.97, 2.11), (3.04, 2.13), (3.11, 2.16), (3.18, 2.19), (3.25, 2.22), (3.32, 2.24), (3.39, 2.27), (3.46, 2.29), (3.54, 2.31), (3.61, 2.33), (3.68, 2.35), (3.75, 2.37), (3.82, 2.39), (3.89, 2.41), (3.96, 2.42), (4.03, 2.44), (4.1, 2.45), (4.17, 2.47), (4.24, 2.48), (4.31, 2.49), (4.38, 2.5), (4.45, 2.51), (4.53, 2.52), (4.6, 2.53), (4.67, 2.53), (4.74, 2.54), (4.81, 2.54), (4.88, 2.55), (4.95, 2.55), (5.02, 2.55), (5.09, 2.55), (5.16, 2.55), (5.23, 2.55), (5.3, 2.55), (5.37, 2.54), (5.44, 2.54), (5.52, 2.53), (5.59, 2.53), (5.66, 2.52), (5.73, 2.51), (5.8, 2.5), (5.87, 2.49), (5.94, 2.48), (6.01, 2.47), (6.08, 2.46), (6.15, 2.44), (6.22, 2.43), (6.29, 2.41), (6.36, 2.39), (6.43, 2.38), (6.51, 2.36), (6.58, 2.34), (6.65, 2.32), (6.72, 2.3), (6.79, 2.27), (6.86, 2.25), (6.93, 2.22), (7.0, 2.2), (7.07, 2.17), (7.14, 2.14), (7.21, 2.11), (7.28, 2.08), (7.35, 2.05), (7.42, 2.02), (7.5, 1.99), (7.57, 1.96), (7.64, 1.92), (7.71, 1.89), (7.78, 1.85), (7.85, 1.81), (7.92, 1.77), (7.99, 1.73), (8.06, 1.69), (8.13, 1.65), (8.2, 1.61), (8.27, 1.57), (8.34, 1.52), (8.41, 1.48), (8.49, 1.43), (8.56, 1.38), (8.63, 1.33), (8.7, 1.28), (8.77, 1.23), (8.84, 1.18), (8.91, 1.13), (8.98, 1.08), (9.05, 1.02), (9.12, 0.968), (9.19, 0.911), (9.26, 0.854), (9.33, 0.796), (9.4, 0.737), (9.48, 0.677), (9.55, 0.616), (9.62, 0.554), (9.69, 0.491), (9.76, 0.427), (9.83, 0.361), (9.9, 0.295), (9.97, 0.229), (10.0, 0.161), (10.1, 0.0916), (10.2, 0.0217)]

X = []
Y = []

for item in path:
    X.append(item[0])
    Y.append(item[1])

plt.ion() # turn interactive mode on
animated_plot = plt.plot(X, Y, 'ro')[0]

for i in range(len(X)):
    animated_plot.set_xdata(X[0:i])
    animated_plot.set_ydata(Y[0:i])
    plt.draw()
    plt.pause(0.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM