我有以下散点图,其中定义了边缘和标记的不同颜色:
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->代理等)。
这是当前的情节(图例为空)

发布于 2020-01-20 06:04:03
这就是最终成功的原因。
classes = ['Agents', 'AI', 'DB', 'IR', 'ML', 'HCI']
class_colours = ['r', 'g', 'b', 'k', 'm', 'y']
recs = []
for i in range(0, len(class_colours)):
recs.append(mpatches.Rectangle((0, 0), 1, 1, fc=class_colours[i]))
plt.legend(recs, classes, loc=4)

发布于 2022-01-12 12:12:21
DsCpp的回答或多或少是可行的,因为您必须创建自定义修补程序,然后将它们传递给您的传奇。简化这一过程的一种方法是为分散函数创建一个自定义包装器,然后属性可以作为kwargs传递,如下所示:
def scatter_wrapper(ax, x, y, **kwargs):
ax.scatter(x, y, **kwargs)
return Line2D([0],[0], linewidth=0, **kwargs)然后在你的主要功能中
fig, ax = plt.subplots()
patches = []
patches.append(scatter_wrapper(ax, x, y, c=c, label="Some data", marker='s'))
ax.legend(handles=patches)只是意味着你可以在一个地方做你的绘图和补丁生成。
https://stackoverflow.com/questions/59812259
复制相似问题