简体   繁体   English

Matplotlib颜色根据类标签

[英]Matplotlib color according to class labels

I have two vectors, one with values and one with class labels like 1,2,3 etc. 我有两个向量,一个带有值,另一个带有1,2,3等类标签。

I would like to plot all the points that belong to class 1 in red, to class 2 in blue, to class 3 in green etc. How can I do that? 我想将所有属于1级的点用红色绘制,用2级用蓝色绘制,用3级用绿色绘制等等。我该怎么做?

The accepted answer has it spot on, but if you might want to specify which class label should be assigned to a specific color or label you could do the following. 已接受的答案是有效的,但是如果您可能想要指定应将哪个类别标签分配给特定颜色或标签,则可以执行以下操作。 I did a little label gymnastics with the colorbar, but making the plot itself reduces to a nice one-liner. 我用彩条做了一个小标签体操,但是让情节本身减少到一个漂亮的单线。 This works great for plotting the results from classifications done with sklearn. 这非常适合绘制使用sklearn完成的分类的结果。 Each label matches a (x,y) coordinate. 每个标签与(x,y)坐标匹配。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = [4,8,12,16,1,4,9,16]
y = [1,4,9,16,4,8,12,3]
label = [0,1,2,3,0,1,2,3]
colors = ['red','green','blue','purple']

fig = plt.figure(figsize=(8,8))
plt.scatter(x, y, c=label, cmap=matplotlib.colors.ListedColormap(colors))

cb = plt.colorbar()
loc = np.arange(0,max(label),max(label)/float(len(colors)))
cb.set_ticks(loc)
cb.set_ticklabels(colors)

散点图颜色标签

Using a slightly modified version of this answer, one can generalise the above for N colors as follows: 使用答案的略微修改版本,可以将上述N种颜色概括如下:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

N = 23 # Number of labels

# setup the plot
fig, ax = plt.subplots(1,1, figsize=(6,6))
# define the data
x = np.random.rand(1000)
y = np.random.rand(1000)
tag = np.random.randint(0,N,1000) # Tag each point with a corresponding label    

# define the colormap
cmap = plt.cm.jet
# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

# define the bins and normalize
bounds = np.linspace(0,N,N+1)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

# make the scatter
scat = ax.scatter(x,y,c=tag,s=np.random.randint(100,500,N),cmap=cmap,     norm=norm)
# create the colorbar
cb = plt.colorbar(scat, spacing='proportional',ticks=bounds)
cb.set_label('Custom cbar')
ax.set_title('Discrete color mappings')
plt.show()

Which gives: 这使:

在此输入图像描述

Assuming that you have your data in a 2d array, this should work: 假设您的数据是二维数组,这应该有效:

import numpy
import pylab
xy = numpy.zeros((2, 1000))
xy[0] = range(1000)
xy[1] = range(1000)
colors = [int(i % 23) for i in xy[0]]
pylab.scatter(xy[0], xy[1], c=colors)
pylab.show()

You can also set a cmap attribute to control which colors will appear through use of a colormap; 您还可以设置cmap属性以控制通过使用色彩图显示哪些颜色; ie replace the pylab.scatter line with: 即用以下pylab.scatter替换pylab.scatter行:

pylab.scatter(xy[0], xy[1], c=colors, cmap=pylab.cm.cool)

A list of color maps can be found here 可以在此处找到颜色映射列表

A simple solution is to assign color for each class. 一个简单的解决方案是为每个类指定颜色。 This way, we can control how each color is for each class. 这样,我们就可以控制每个类的每种颜色。 For example: 例如:

arr1 = [1, 2, 3, 4, 5]
arr2 = [2, 3, 3, 4, 4]
labl = [0, 1, 1, 0, 0]
color= ['red' if l == 0 else 'green' for l in labl]
plt.scatter(arr1, arr2, color=color)

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

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