如何绘制不同颜色的动画点与matplotlib? [英] How to plot animated dots in different colors with matplotlib?

查看:326
本文介绍了如何绘制不同颜色的动画点与matplotlib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数生成动画点,这里是导致问题的部分:

I have a function that generates animated dots, here is the part that causes a problem :

dots = [dot() for i in range(N)] 

fig = plt.figure()  
ax = plt.axes(xlim=(0, 10), ylim=(0, 10)) 
d, = ax.plot([dot.x for dot in dots],[dot.y for dot in dots], 'ro', markersize=3)`

所以,dot是我的类对象的名称,点是包含N个对象的列表。

so, dot is the name of my class of objects et dots is the list that contains N objects. Every dot is plotted in red.

我想做的是绘制N-1个点为红色,一个点为蓝色,这是可能的命令ax.plot?

What I want to do is to plot, for example, N-1 dots in red and one dot in blue, is it possible with the command ax.plot ?

感谢您的帮助

推荐答案

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

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()

这篇关于如何绘制不同颜色的动画点与matplotlib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆