如何使用 seaborn 解决具有相同数值的条的颜色密度问题?

How to fix color density issue for bars with the same numeric value using seaborn?

我有这个代码:

import numpy as np, matplotlib.pyplot as plt, seaborn as sns


fig = plt.figure(1)
sns.set(style="whitegrid", color_codes=True)
data=departments.value_counts()[:10]
pal = sns.color_palette("Blues_d", len(data))
rank = data.argsort().argsort()   
sns.barplot(y=data.index, x=data, palette=np.array(pal[::1])[rank])
plt.title("Top departments", y=1.02)

fig.savefig('Top_departments', bbox_inches='tight', dpi=300)

我的数据是这样的:

Chemistry                                19
Computer Science                         13
Physics                                  10
Epidemiology and Biostatistics            8
Psychology                                8
Mechanical and Industrial Engineering     8
Sociology                                 7
Academic                                  7
Bioengineering                            7
Biochemistry and Molecular Genetics       6

剧情附后。问题是我得到的条具有不同的蓝色阴影,比如:社会学、学术和生物工程,而它们应该是相同的阴影,因为它们具有相同的数值。

问题的代码似乎将顺序颜色列表分配给从大到小的值。当遇到 ex aequos 时,顺序没有明确定义,但该方法将始终分配不同的颜色,尽管值是相等的。该方法似乎是为了在条形图尚未排序且所有值都不同时从高到低为条形图着色。

这是一种按值着色的方法:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

fig = plt.figure(1)
sns.set(style="whitegrid", color_codes=True)
data = pd.Series([19, 13, 10, 8, 8, 8, 7, 7, 7, 6],
                 index=['Chemistry', 'Computer Science', 'Physics', 'Epidemiology and Biostatistics',
                        'Psychology', 'Mechanical and Industrial Engineering', 'Sociology',
                        'Academic', 'Bioengineering', 'Biochemistry and Molecular Genetics'])
cmap = sns.color_palette("Blues_d", as_cmap=True)
norm = plt.Normalize(data.min(), data.max())
sns.barplot(y=data.index, x=data, palette=cmap(norm(data.values)))
plt.title("Top departments", y=1.02)
plt.tight_layout()
plt.show()

要将条形从深到浅着色,您可以直接使用反向调色板而不涉及 argsort:

pal = sns.color_palette("Blues_r_d", len(data))
sns.barplot(y=data.index, x=data, palette=pal)
plt.title("Top departments", y=1.02)