如何使用 Matplotlib 或 Seaborn 指定基于不同组的图例
How to specify legend based on different groups with Matplotlib or Seaborn
我有一个如下所示的数据集:
df = {'tic': {0: 'A',
1: 'AAPL',
2: 'ABC',
3: 'ABT',
4: 'ADBE',
5: 'ADI',
6: 'ADM',
7: 'ADP',
8: 'ADSK',
9: 'AEE'},
'Class': {0: 'Manufacturing',
1: 'Tech',
2: 'Trade',
3: 'Manufacturing',
4: 'Services',
5: 'Tech',
6: 'Manufacturing',
7: 'Services',
8: 'Services',
9: 'Electricity and Transportation'},
'Color': {0: 'blue',
1: 'teal',
2: 'purple',
3: 'blue',
4: 'red',
5: 'teal',
6: 'blue',
7: 'red',
8: 'red',
9: 'orange'},
'Pooled 1': {0: 0.0643791550056838,
1: 0.05022103288830682,
2: 0.039223739393748916,
3: 0.036366693834970217,
4: 0.05772708899447428,
5: 0.05969899935101172,
6: 0.04568101605219955,
7: 0.04542272002937567,
8: 0.07138013872431757,
9: 0.029987722053015278}}
我想用 Pooled 1
中存储的值生成蝙蝠图。但我想用 Color
中存储的颜色为条形图着色。相同 Class
的所有条形应该具有相同的颜色并且应该一起绘制。我只展示了上面数据集的一部分。
我使用的代码如下:
fig, axs = plt.subplots(1,1,figsize = (24, 5))
tmp_df = df.sort_values('Class')
plt.bar(np.arange(len(df)), tmp_df['Pooled 1'], color = tmp_df['Color'])
它几乎可以产生所需的输出:
我想要一个图例,其中包含 Class
中的名称和 Color
中的颜色。我知道 seaborn 可以用 barplot
做到这一点,但它不会遵循所需的颜色。而且我不知道为什么 barplot
需要很长时间来绘制数据集。不过 Matplotlib 超级快。
在这种情况下添加图例的最佳方法是什么?提前致谢!
您可以为每个 class 的第一个栏指定一个标签。 Matplotlib 将使用这些标签来创建图例:
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({'tic': {0: 'A', 1: 'AAPL', 2: 'ABC', 3: 'ABT', 4: 'ADBE', 5: 'ADI', 6: 'ADM', 7: 'ADP', 8: 'ADSK', 9: 'AEE'}, 'Class': {0: 'Manufacturing', 1: 'Tech', 2: 'Trade', 3: 'Manufacturing', 4: 'Services', 5: 'Tech', 6: 'Manufacturing', 7: 'Services', 8: 'Services', 9: 'Electricity and Transportation'}, 'Color': {0: 'blue', 1: 'teal', 2: 'purple', 3: 'blue', 4: 'red', 5: 'teal', 6: 'blue', 7: 'red', 8: 'red', 9: 'orange'}, 'Pooled 1': {0: 0.0643791550056838, 1: 0.05022103288830682, 2: 0.039223739393748916, 3: 0.036366693834970217, 4: 0.05772708899447428, 5: 0.05969899935101172, 6: 0.04568101605219955, 7: 0.04542272002937567, 8: 0.07138013872431757, 9: 0.029987722053015278}})
fig, ax = plt.subplots(1, 1, figsize=(14, 5))
tmp_df = df.sort_values('Class')
bars = ax.bar(tmp_df['tic'], tmp_df['Pooled 1'], color=tmp_df['Color'])
prev = None
for cl, color, bar in zip(tmp_df['Class'], tmp_df['Color'], bars):
if cl != prev:
bar.set_label(cl)
prev = cl
ax.margins(x=0.01)
ax.legend(title='Class', bbox_to_anchor=(1.01, 1.01), loc='upper left')
plt.tight_layout()
plt.show()
PS:请注意,您也可以使用 Seaborn 并让着色自动进行:
import seaborn as sns
sns.barplot(data=tmp_df, x='tic', y='Pooled 1', hue='Class', palette='tab10', dodge=False, saturation=1, ax=ax)
我有一个如下所示的数据集:
df = {'tic': {0: 'A',
1: 'AAPL',
2: 'ABC',
3: 'ABT',
4: 'ADBE',
5: 'ADI',
6: 'ADM',
7: 'ADP',
8: 'ADSK',
9: 'AEE'},
'Class': {0: 'Manufacturing',
1: 'Tech',
2: 'Trade',
3: 'Manufacturing',
4: 'Services',
5: 'Tech',
6: 'Manufacturing',
7: 'Services',
8: 'Services',
9: 'Electricity and Transportation'},
'Color': {0: 'blue',
1: 'teal',
2: 'purple',
3: 'blue',
4: 'red',
5: 'teal',
6: 'blue',
7: 'red',
8: 'red',
9: 'orange'},
'Pooled 1': {0: 0.0643791550056838,
1: 0.05022103288830682,
2: 0.039223739393748916,
3: 0.036366693834970217,
4: 0.05772708899447428,
5: 0.05969899935101172,
6: 0.04568101605219955,
7: 0.04542272002937567,
8: 0.07138013872431757,
9: 0.029987722053015278}}
我想用 Pooled 1
中存储的值生成蝙蝠图。但我想用 Color
中存储的颜色为条形图着色。相同 Class
的所有条形应该具有相同的颜色并且应该一起绘制。我只展示了上面数据集的一部分。
我使用的代码如下:
fig, axs = plt.subplots(1,1,figsize = (24, 5))
tmp_df = df.sort_values('Class')
plt.bar(np.arange(len(df)), tmp_df['Pooled 1'], color = tmp_df['Color'])
它几乎可以产生所需的输出:
我想要一个图例,其中包含 Class
中的名称和 Color
中的颜色。我知道 seaborn 可以用 barplot
做到这一点,但它不会遵循所需的颜色。而且我不知道为什么 barplot
需要很长时间来绘制数据集。不过 Matplotlib 超级快。
在这种情况下添加图例的最佳方法是什么?提前致谢!
您可以为每个 class 的第一个栏指定一个标签。 Matplotlib 将使用这些标签来创建图例:
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({'tic': {0: 'A', 1: 'AAPL', 2: 'ABC', 3: 'ABT', 4: 'ADBE', 5: 'ADI', 6: 'ADM', 7: 'ADP', 8: 'ADSK', 9: 'AEE'}, 'Class': {0: 'Manufacturing', 1: 'Tech', 2: 'Trade', 3: 'Manufacturing', 4: 'Services', 5: 'Tech', 6: 'Manufacturing', 7: 'Services', 8: 'Services', 9: 'Electricity and Transportation'}, 'Color': {0: 'blue', 1: 'teal', 2: 'purple', 3: 'blue', 4: 'red', 5: 'teal', 6: 'blue', 7: 'red', 8: 'red', 9: 'orange'}, 'Pooled 1': {0: 0.0643791550056838, 1: 0.05022103288830682, 2: 0.039223739393748916, 3: 0.036366693834970217, 4: 0.05772708899447428, 5: 0.05969899935101172, 6: 0.04568101605219955, 7: 0.04542272002937567, 8: 0.07138013872431757, 9: 0.029987722053015278}})
fig, ax = plt.subplots(1, 1, figsize=(14, 5))
tmp_df = df.sort_values('Class')
bars = ax.bar(tmp_df['tic'], tmp_df['Pooled 1'], color=tmp_df['Color'])
prev = None
for cl, color, bar in zip(tmp_df['Class'], tmp_df['Color'], bars):
if cl != prev:
bar.set_label(cl)
prev = cl
ax.margins(x=0.01)
ax.legend(title='Class', bbox_to_anchor=(1.01, 1.01), loc='upper left')
plt.tight_layout()
plt.show()
PS:请注意,您也可以使用 Seaborn 并让着色自动进行:
import seaborn as sns
sns.barplot(data=tmp_df, x='tic', y='Pooled 1', hue='Class', palette='tab10', dodge=False, saturation=1, ax=ax)