python颜色列表_python-根据类标签的Matplotlib颜色

可以接受的答案可以找到答案,但是如果您想指定应该将哪种类别标签分配给特定的颜色或标签,则可以执行以下操作。 我使用色标进行了一些标签体操训练,但制作情节本身却减少了线条。 这对于绘制sklearn进行分类的结果非常有用。 每个标签都匹配一个(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)

使用此答案的稍作修改的版本,可以将上述内容概括为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()

这使:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值