问题描述
我正在使用 seaborn
和 pandas
从不同(但相关)的数据创建一些条形图.这两个数据集共享一个用作 hue
的公共类别,因此我想确保在两个图中该类别的条形颜色匹配.我该怎么办?
I'm using seaborn
and pandas
to create some bar plots from different (but related) data. The two datasets share a common category used as a hue
, and as such I would like to ensure that in the two graphs the bar colour for this category matches. How can I go about this?
一个基本的例子如下:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
sns.set_style('darkgrid')
fig, ax = plt.subplots()
a = pd.DataFrame({'Program': ['A', 'A', 'B', 'B', 'Total', 'Total'],
'Scenario': ['X', 'Y', 'X', 'Y', 'X', 'Y'],
'Duration': [4, 3, 5, 4, 9, 7]})
g = sns.barplot(data=a, x='Scenario', y='Duration',
hue='Program', ci=None)
plt.tight_layout()
plt.savefig('3 progs.png')
plt.clf()
b = pd.DataFrame({'Program': ['A', 'A', 'B', 'B', 'C', 'C', 'Total', 'Total'],
'Scenario': ['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y'],
'Duration': [4, 3, 5, 4, 3, 2, 12, 9]})
g = sns.barplot(data=b, x='Scenario', y='Duration',
hue='Program', ci=None)
plt.tight_layout()
plt.savefig('4 progs.png')
生成两个图:
在这个例子中,我想确保 Total
类别在两个图表中使用相同的颜色(例如黑色)
In this example, I would like to ensure that the Total
category uses the same colour in both graphs (e.g. black)
推荐答案
A.使用颜色列表
确保两个图中相同类别具有相同颜色的最简单解决方案是在创建图时手动指定颜色.
A. using a list of colors
The easiest solution to make sure to have the same colors for the same categories in both plots would be to manually specify the colors at plot creation.
# First bar plot
ax = sns.barplot(data=a, x='Scenario', y='Duration',
hue='Program', ci=None, palette=["C0", "C1", "k"])
# ...
# Second bar plot
ax2 = sns.barplot(data=b, x='Scenario', y='Duration',
hue='Program', ci=None, palette=["C0", "C1", "C2", "k"])
颜色 C2"
(颜色循环的第三种颜色)仅出现在存在程序 C 的第二个图中.
The color "C2"
(the third color of the color cycle) is only present in the second plot where there exists a Programm C.
除了列表,您还可以使用字典,将 hue
列中的值映射到颜色.
Instead of a list, you may also use a dictionary, mapping values from the hue
column to colors.
palette ={"A": "C0", "B": "C1", "C": "C2", "Total": "k"}
ax = sns.barplot(data=a, x='Scenario', y='Duration', hue='Program', palette=palette)
# ...
ax2 = sns.barplot(data=b, x='Scenario', y='Duration', hue='Program', palette=palette)
在这两种情况下,输出将如下所示:
In both cases, the output would look like this:
最后,您可以根据 hue
列中的值自动创建此字典.这里的优点是您既不需要事先知道颜色,也不需要知道相应数据框中的值.
Finally, you may create this dictionary automatically from the values from the hue
column. The advantage here would be that you neither need to know the colors, nor the values in the respective dataframes beforehands.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
sns.set_style('darkgrid')
fig, ax = plt.subplots()
a = pd.DataFrame({'Program': ['A', 'A', 'B', 'B', 'Total', 'Total'],
'Scenario': ['X', 'Y', 'X', 'Y', 'X', 'Y'],
'Duration': [4, 3, 5, 4, 9, 7]})
b = pd.DataFrame({'Program': ['A', 'A', 'B', 'B', 'C', 'C', 'Total', 'Total'],
'Scenario': ['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y'],
'Duration': [4, 3, 5, 4, 3, 2, 12, 9]})
unique = a["Program"].append(b["Program"]).unique()
palette = dict(zip(unique, sns.color_palette(n_colors=len(unique))))
palette.update({"Total":"k"})
ax = sns.barplot(data=a, x='Scenario', y='Duration',
hue='Program', ci=None, palette=palette)
plt.tight_layout()
plt.figure()
ax2 = sns.barplot(data=b, x='Scenario', y='Duration',
hue='Program', ci=None, palette=palette)
plt.tight_layout()
plt.show()
这篇关于Seaborn - 根据色调名称更改条形颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!