matplotlib legend_elements返回空

问题描述 投票:0回答:1

我有以下散点图,其中为边缘和标记定义了不同的颜色:

    label_colors = [mcolors.to_rgba('rgbkmy'[lbl]) for lbl in list(labels)]
    pred_colors = [mcolors.to_rgba('rgbkmy'[lbl]) for lbl in list(preds)]

    classes = ['Agents','AI','DB','IR','ML','HCI']

    fig, ax = plt.subplots()

    scatter = plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=label_colors, edgecolors=pred_colors, linewidths=2)


    legend1 = ax.legend(*scatter.legend_elements(),
                        loc="lower left", title="Classes")
    ax.add_artist(legend1)

    if(is_show):plt.show()

[不幸的是,scatter.legend_elements()返回一个空列表,如果我将其更改为c=labels,这是一个整数列表,我可以绘制图例,但是edgecolors会中断,因为edgecolors需要RGBA作为输入。

我的目标是创建一个图例,其颜色与label_colors,edgecolors中显示的颜色相同,并且图例中的标签本身将与classes相对应(0->代理等)。这是当前情节(图例为空)enter image description here

python matplotlib
1个回答
0
投票

这是最终成功的方法

© www.soinside.com 2019 - 2024. All rights reserved.