如何用每个点分别指定颜色创建散点图?

时间:2019-08-06 13:38:00

标签: python matplotlib colors

使用matplotlib,我创建了一个散点图动画,该动画每秒钟显示一个新点,并显示部分透明的所有旧点。每个点由xy定义,但也由类别s定义。我希望将点的颜色绑定到其类别。理想情况下,这意味着数组s包含值1、2和3,并且分别定义了属于那些值的颜色。但是,我无法使它正常工作。

我要做的是在s中分别指定每个点的边缘颜色,其代码如下所示。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as plti
import matplotlib.animation

s = [[1,0,0],[0,1,0],[0,0,1]];
x = [525,480,260];
y = [215,180,180];

img = plti.imread('myimage.png')
fig, ax = plt.subplots()
plt.imshow(img)
plt.axis('off')

x_vals = []
y_vals = []
intensity = []
iterations = len(x)
colors = []

t_vals = np.linspace(0,iterations-1,iterations,dtype=int)
scatter = ax.scatter(x_vals, y_vals, s=100, c=colors, vmin=0, vmax=1)

def init():
    pass

def update(t):
    global x, y, x_vals, y_vals, intensity
    x_vals.extend([x[t]])
    y_vals.extend([y[t]])
    scatter.set_offsets(np.c_[x_vals,y_vals])
    intensity = np.concatenate((np.array(intensity), np.ones(1)))
    if len(intensity) > 1:
        intensity[-2] = 0.5
    scatter.set_array(intensity)
    colors.extend([s[t]])
    scatter.set_color(colors)

    return ani

ani = matplotlib.animation.FuncAnimation(fig, update, frames=t_vals, interval=1000, repeat=False, init_func=init)
plt.show()

仅将c=colors更改为facecolor=colors无效。另外,我尝试使用颜色图,但是我也无法使用它。

上面的代码生成的动画如下所示。

enter image description here

但是,动画应该看起来像这样。

enter image description here

所以我的问题是;有人知道如何将每个点的面色与该点所属的类别联系起来吗?

2 个答案:

答案 0 :(得分:2)

在matplotlib中用不同颜色的点绘制图的正常方法是传递颜色列表作为参数。

例如:

import matplotlib.pyplot
matplotlib.pyplot.scatter([1,2,3],[4,5,6],color=['red','green','blue'])

enter image description here

但是,如果由于某种原因您只想打一个电话,就可以制作一个大的颜色列表,并具有列表理解力和一些地板分割:

import matplotlib
import numpy as np

X = [1,2,3,4]
Ys = np.array([[4,8,12,16],
      [1,4,9,16],
      [17, 10, 13, 18],
      [9, 10, 18, 11],
      [4, 15, 17, 6],
      [7, 10, 8, 7],
      [9, 0, 10, 11],
      [14, 1, 15, 5],
      [8, 15, 9, 14],
       [20, 7, 1, 5]])
nCols = len(X)  
nRows = Ys.shape[0]

colors = matplotlib.cm.rainbow(np.linspace(0, 1, len(Ys)))

cs = [colors[i//len(X)] for i in range(len(Ys)*len(X))] #could be done with numpy's repmat
Xs=X*nRows #use list multiplication for repetition
matplotlib.pyplot.scatter(Xs,Ys.flatten(),color=cs)

enter image description here

答案 1 :(得分:1)

发生问题是因为在scatter.set_array(intensity)之前调用了行scatter.set_color(colors)。因此,它不是通过单独的变量定义强度,而是直接集成到颜色中。以下代码可产生预期的结果。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as plti
import matplotlib.animation

s = [1,2,3];
x = [525,480,260];
y = [215,180,180];

img = plti.imread('myimage.png')
fig, ax = plt.subplots()
plt.imshow(img)
plt.axis('off')

x_vals = []
y_vals = []
iterations = len(x)
colors = []

t_vals = np.linspace(0,iterations-1,iterations,dtype=int)
scatter = ax.scatter(x_vals, y_vals, s=100, color=colors, vmin=0, vmax=1)

def init():
    pass

def update(t):
    global x, y, x_vals, y_vals
    x_vals.extend([x[t]])
    y_vals.extend([y[t]])
    scatter.set_offsets(np.c_[x_vals,y_vals])

    if t > 0:
        if s[t-1] == 1:
            colors[t-1] = [1,0,0,0.5];
        elif s[t-1] == 2:
            colors[t-1] = [0,1,0,0.5];
        else:
            colors[t-1] = [0,0,1,0.5];

    if s[t] == 1:
        colors.extend([[1,0,0,1]])
    elif s[t] == 2:
        colors.extend([[0,1,0,1]])
    else:
        colors.extend([[0,0,1,1]])
    scatter.set_color(colors);

    return ani

ani = matplotlib.animation.FuncAnimation(fig, update, frames=t_vals, init_func=init, interval=1000, repeat=False)
plt.show()