matplotlib scatter的legend和edgecolors

可视化 feature space,每个类一种颜色,类中心用空心星星表示,并且画出图示表明每种颜色代表的类。

facecolors='none' 设置空心点,edgecolors 指定边颜色,但能用 label sequence + cmap 的方式指定每个点的颜色,即:

import matplotlib.pyplot as plt

x = y = label = list(range(5))
plt.scatter(x, y, facecolors='none', edgecolors=label, cmap="tab20")  # 报错
plt.show()

这样用会报错:

ValueError: Invalid RGBA argument: 0

要自己先处理出颜色序列,再传给 edgecolors

import matplotlib.pyplot as plt

# 选定 color map
cmap = plt.get_cmap('tab20')
# `Qualitative colormaps` 颜色种类有限
n_color = len(cmap.colors)
# 其中 `tab20` 就只有 `20` 种
print("#colours:", n_color)  # 20
print("first colour:", cmap(0))  # 应该是 (R, G, B, A) 值

fig, ax = plt.subplots()
# 故意多出两组
for c in range(n_color + 2):
    # 2 points for each colour
    x = [2 * c + 1, 2 * c + 2]
    y = x
    # the value of this colour
    _c_val = cmap(c)  # 或 cmap.colors[c]
    # 可以看到,最后多出的两组,颜色和第 20 组是一样的,因为颜色不够用了
    print("colour:", _c_val)
    # colour list for these 2 points
    # 自己处理颜色序列
    _c_ls = [_c_val] * len(x)
    if c % 2 == 0:
    	# 实心点
        plt.scatter(x, y, c=_c_ls, label=c)
    else:
    	# 空心星星
    	# 这样传 `_c_ls` 给 `edgecolors` 就可以
        plt.scatter(x, y, marker="*", facecolors='none', edgecolors=_c_ls, label=c)

plt.legend(fontsize=7)
# plt.show()
fig.savefig("test.png")
plt.close(fig)
  • 输出,最后 3 种颜色相同。
#colours: 20
first colour: (0.12156862745098039, 0.4666666666666667, 0.7058823529411765, 1.0)
colour: (0.12156862745098039, 0.4666666666666667, 0.7058823529411765, 1.0)
colour: (0.6823529411764706, 0.7803921568627451, 0.9098039215686274, 1.0)
colour: (1.0, 0.4980392156862745, 0.054901960784313725, 1.0)
colour: (1.0, 0.7333333333333333, 0.47058823529411764, 1.0)
colour: (0.17254901960784313, 0.6274509803921569, 0.17254901960784313, 1.0)
colour: (0.596078431372549, 0.8745098039215686, 0.5411764705882353, 1.0)
colour: (0.8392156862745098, 0.15294117647058825, 0.1568627450980392, 1.0)
colour: (1.0, 0.596078431372549, 0.5882352941176471, 1.0)
colour: (0.5803921568627451, 0.403921568627451, 0.7411764705882353, 1.0)
colour: (0.7725490196078432, 0.6901960784313725, 0.8352941176470589, 1.0)
colour: (0.5490196078431373, 0.33725490196078434, 0.29411764705882354, 1.0)
colour: (0.7686274509803922, 0.611764705882353, 0.5803921568627451, 1.0)
colour: (0.8901960784313725, 0.4666666666666667, 0.7607843137254902, 1.0)
colour: (0.9686274509803922, 0.7137254901960784, 0.8235294117647058, 1.0)
colour: (0.4980392156862745, 0.4980392156862745, 0.4980392156862745, 1.0)
colour: (0.7803921568627451, 0.7803921568627451, 0.7803921568627451, 1.0)
colour: (0.7372549019607844, 0.7411764705882353, 0.13333333333333333, 1.0)
colour: (0.8588235294117647, 0.8588235294117647, 0.5529411764705883, 1.0)
colour: (0.09019607843137255, 0.7450980392156863, 0.8117647058823529, 1.0)
colour: (0.6196078431372549, 0.8549019607843137, 0.8980392156862745, 1.0)
colour: (0.6196078431372549, 0.8549019607843137, 0.8980392156862745, 1.0)
colour: (0.6196078431372549, 0.8549019607843137, 0.8980392156862745, 1.0)

test.png

automated legend creation

新版 matplotlib(3.4.2)可以用 [2] 的方法自动创建 scatter 的 legend label,但我这用的旧版(3.0.3)不行,只能自己分类打 label。

References

  1. Choosing Colormaps in Matplotlib
  2. Scatter plots with a legend
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值