且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何绘制不同颜色的动画点与matplotlib?

更新时间:2023-01-27 09:18:58

是的,这是可能的。你需要将点分成两个集合;有很多方法可以做到这一点;这里我选择从列表中提取一个点。那么您必须在同一画布上分别绘制每个集合。

Yes, it is possible. You will need to segregate the points into two collections; there are a number of ways to do this; here I chose to extract one point from the list. then you must plot each collections separately on the same canvas.

import random
import matplotlib.pyplot as plt


class Dot(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

def get_random_dot(dots):
    random.shuffle(dots)
    return dots.pop()

num_dots = 10
dots = [Dot(random.random(), random.random()) for _ in range(num_dots)] 

fig = plt.figure()  
ax = plt.axes() 

selected_dot = get_random_dot(dots)
d, = ax.plot([dot.x for dot in dots],[dot.y for dot in dots], 'r.')
f, = ax.plot(selected_dot.x, selected_dot.y, color='blue', marker='o', linewidth=3)

plt.show()