为具有特定颜色的 matplotlib 散点图创建颜色编码键

Create a color-coded key for a matplotlib scatter plot with specific colors

这是数据:

import pandas as pd

data = {'letter': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'], 'color': ['#FF0000', '#FF7F00', '#FFD400', '#FFFF00', '#BFFF00', '#6AFF00', '#00EAFF', '#0095FF', '#0040FF', '#AA00FF', '#FF00AA', '#EDB9B9', '#E7E9B9', '#B9EDE0', '#B9D7ED', '#DCB9ED', '#8F2323', '#8F6A23', '#4F8F23', '#23628F', '#6B238F', '#000000', '#737373', '#CCCCCC'], 'percent': [0.59, 0.569, 0.343, 0.791, 0.099, 0.047, 0.387, 0.232, 0.262, 0.177, 0.522, 0.317, 0.252, 0.617, 0.644, 0.571, 0.382, 0.12, 0.281, 0.855, 0.283, 1.0, 0.844, 0.499], 'score': [0.541, 0.399, 0.625, 0.584, 0.83, 0.859, 0.62, 0.618, 0.545, 0.536, 0.513, 0.563, 0.592, 0.276, 0.037, 0.0, 0.5, 0.653, 0.485, 0.213, 0.44, 0.0, 0.308, 0.35]}
df = pd.DataFrame(data)

# display(df.head())
  letter    color  percent  score
0      A  #FF0000    0.590  0.541
1      B  #FF7F00    0.569  0.399
2      C  #FFD400    0.343  0.625
3      D  #FFFF00    0.791  0.584
4      E  #BFFF00    0.099  0.830

其中最左边的列是索引。

此代码创建散点图:

df.plot.scatter(x='percent', y='score', color=df['color'])

在右边,我想要一个键指定哪种颜色代表哪个字母。理想情况下,它应该是纯色矩形和字母的列表。我一直无法找到可以使用他们选择的颜色的解决方案,但我需要这种行为,因为会有多个图需要以相同的方式进行颜色编码。

可以使用Axes对象的.legend方法:

import matplotlib.lines as mlines

ax = df.plot.scatter(x='percent', y='score', color=df['color'])

ax.legend(handles=[mlines.Line2D([], [], color=r['color'], marker='.', linestyle='None',
                                 markersize=9, label=r['letter'])
                   for _, r in df.iterrows()
                  ],
          ncol=3,
          bbox_to_anchor=(1, 1)
         )

注意。在我看来,你不应该超过 ~10 个彩色项目,这会严重影响可读性

由于每种颜色只有一个元素,因此您可以直接注释这些点:

ax = df.plot.scatter(x='percent', y='score', color=df['color'])

for _, r in df.iterrows():
    ax.annotate(r['letter'], (r['percent'], r['score']), ha='left', va='bottom')

您可以使用 mpatches.Patch 作为自定义图例。

import matplotlib.patches as mpatches

ax = df.plot.scatter(x='percent', y='score', color=df['color'])

colorlist = zip(df['letter'], df['color'])
handles = [mpatches.Patch(color=colour, label=label) for label, colour in colorlist]
labels = df['letter']

ax.legend(handles, labels, ncol=2, bbox_to_anchor=(1, 1))

或者,您可以使用 seaborn

import seaborn as sns

ax = sns.scatterplot(x=df['percent'], y=df['score'], palette=df['color'].tolist(), hue=df['letter'])
ax.legend(ncol=2, bbox_to_anchor=(1, 1))