Matplotlib 按颜色绘制分类图

Matplotlib 按颜色绘制分类图,matplotlib,Matplotlib,我正在绘制一个分割图,遇到了一个问题,我的分类值只显示一种颜色。有什么建议吗 [![fig = plt.figure(figsize = (20,10)) ax = fig.add_subplot(1,1,1) ax.set_xlabel('Sentiment Score', fontsize = 15) ax.set_ylabel('Star Review', fontsize = 15) ax.set_title('Heinz Segmentation', fontsize = 2

我正在绘制一个分割图,遇到了一个问题,我的分类值只显示一种颜色。有什么建议吗

    [![fig = plt.figure(figsize = (20,10))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Sentiment Score', fontsize = 15)
ax.set_ylabel('Star Review', fontsize = 15)
ax.set_title('Heinz Segmentation', fontsize = 20)


targets = \['Sugar/ Healthy Positives', 'Sugar/ Healthy Negatives', 'Price/ Value Positives', 'Price/ Value Negatives', 'Purists Positives', 'Purists Negatives'\]
colors = \['r', 'g', 'b', 'c', 'm', 'y'\]
for target, color in zip(targets,colors):
    ax.scatter(df.Compound,df.StarsInt
               , c = color
               , s = 200
              ,alpha = .5)
ax.legend(targets, prop={'size': 14})
ax.grid()][1]][1]

IIUC,你在传说中遇到了问题。绘图时忘记传递图例

for target, color in zip(targets,colors):
    ax.scatter(df.Compound,df.StarsInt, c=color,
               s = 200,alpha = .5,       
               label=target) # <--- pass the label to show in legend
对于目标,zip中的颜色(目标,颜色):
最大散射(df.化合物,df.起始,c=颜色,
s=200,α=0.5,
标签=目标)#