Matplotlib:散点图中标记和颜色的图例

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

我正在尝试绘制数据框的散点图(发布在下面)。我想表示散点图,不同阶段显示为不同标记(圆形、方形等),不同产品显示为不同颜色(红色、蓝色等)。到目前为止,我已经做到了,但我很难展示描述这一点的图例。

这是我写的:

df = pd.DataFrame([[1500,24,'open','drive'],
                   [2900, 30, 'open', 'walk'],
                   [1200, 50, 'closed', 'drive'],
                   [4000, 80, 'open', 'air'],
                   [8000, 70, 'ongoing', 'air'],
                   [6100, 40, 'ongoing', 'walk'],
                   [7200, 85, 'closed', 'drive'],
                   [3300, 25, 'closed', 'drive'],
                   [5400, 45, 'open', 'walk'],
                   [5900, 53, 'open', 'air']])
df.columns = ['Cost','Duration','Stage','Product']

label_encoder = LabelEncoder()
markers = {0: 'o', 1: 's', 2: '^'}
df['Product_encoded'] = label_encoder.fit_transform(df['Product'])
df['Stage_encoded'] = label_encoder.fit_transform(df['Stage'])
df['Stage_encoded']= df['Stage_encoded'].map(markers)

X= np.array(df)
for idx,cl in enumerate(np.unique(df['Stage_encoded'])):
    plt.scatter(x=X[df['Stage_encoded']== cl,0],y=X[df['Stage_encoded']== cl,1],marker=cl,c=[colors[i] for i in X[df['Stage_encoded'] == cl, 4]])
    plt.legend()

这显示了绘图并为该点提供了适当的颜色和标记,但我想显示图例(标记和颜色)

python pandas matplotlib scatter-plot
1个回答
0
投票

我调整了 for 循环,使其更容易处理:

for stage in df["Stage"].unique(): # for every unique stage
    subD = df[df["Stage"]==stage] # get the data for that specific case
    plt.scatter(x=subD["Cost"], # cost on x axis
                y=subD["Duration"], # duration on y aaxis
                marker=subD["Stage_encoded"].unique()[0], # marker from what you defined
                # color will be automatically changing, no need to specify it in this case
                label=subD['Stage_encoded'].unique()[0]) # add a label!
plt.legend() # legend on the outside! otherwise it will turn on or off each loop!

这是情节,带有图例;)

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