简体   繁体   English

如何在 python 中使用 matplotlib 为图中的每个散点获取不同的标签名称?

[英]How to get distinct label name for each scatter points in graph using matplotlib in python?

I am using a for loop for giving labels & color to points.我正在使用 for 循环为点提供标签和颜色。 But I am not getting a unique label for each different color symbol, instead, I am getting a long queue at the top of my graph due to large excel data of 500 or so rows.但是我没有得到每个不同颜色符号的唯一标签,相反,由于 500 行左右的大型 Excel 数据,我在图表顶部排起了长队。 Here is my code:这是我的代码:

for val in ghi:
if val < 2:
    col.append('navy')
    lab.append(' <2 ')
elif (val >= 2) and (val < 4):
    col.append('lightblue')
    lab.append(' 2~4 ')
elif (val >= 4) and (val < 6):
    col.append('orange')
    lab.append(' 4~6 ')
else:
    col.append('brown')
    lab.append(' >6 ')
plt.scatter(x, y, marker = "D", s = 10, color = col, label = lab)
sns.lineplot(x, y, color ="red")
plt.legend()

The reason you are seeing multiple labels is because of label=lab .您看到多个标签的原因是label=lab There is one entry for each row.每行有一个条目。 So, you will need to use plt.legend() to add the handles and labels, so that you can have just four entries as in the if/else statement.因此,您将需要使用plt.legend()添加句柄和标签,这样您就可以像 if/else 语句中那样只有四个条目。 Also, to ensure the colors and labels match, sort the data by the lab field.此外,为确保颜色和标签匹配,请按实验室字段对数据进行排序。

As there wasn't any data provided the first few lines include creation of random data.由于没有提供任何数据,前几行包括随机数据的创建。 Note that I have the data into a dataframe and sorted it by ghi field.请注意,我将数据放入数据框中并按ghi字段对其进行排序。 Later, the legend has the handles and labels that you can customize as you need.稍后,图例具有您可以根据需要自定义的句柄和标签。 Hope this is what you are trying to achieve.希望这是您想要实现的目标。

Otherwise, I have tried to keep the same names and as much of your code as is, so that it is easier for you to interpret.否则,我已尝试保持相同的名称和尽可能多的代码,以便您更容易解释。

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.lines import Line2D 

ghi = np.random.randint(11, size=(20))
x = np.random.rand(20)
y = np.random.rand(20)
col = []
for val in ghi:
    if val < 2:
        col.append('navy')
    elif (val >= 2) and (val < 4):
        col.append('lightblue')
    elif (val >= 4) and (val < 6):
        col.append('orange')
    else:
        col.append('brown')

df = pd.DataFrame({'x':x, 'y':y, 'ghi':ghi, 'col':col})
df.sort_values(by=['ghi'], inplace = True)
plt.scatter(df.x, df.y, marker = "D", s = 50, color = df.col)#, label = np.unique(lab))
sns.lineplot(x, y, color ="red")
myHandles = [Line2D([], [], marker='D', color='navy', linestyle='None'),
          Line2D([], [], marker='D', color='lightblue', linestyle='None'),
          Line2D([], [], marker='D', color='orange', linestyle='None'),
          Line2D([], [], marker='D', color='brown', linestyle='None')]

plt.legend(handles=myHandles, labels = [' <2 ', ' 2~4 ', ' 4~6 ', ' >6 '])

Output plot输出图

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM