按颜色绘制分类



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

[![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

最新更新