The countplot有一个饱和度参数(饱和度越高,颜色越“真实”,饱和度越低,颜色越接近灰色)。 Seaborn 在条形图中使用饱和度来使默认颜色看起来“更平滑”。默认饱和度为0.75
;它可以设置为1
获得“真实”颜色。
The histplot
有一个 alpha 参数,使颜色半透明。颜色与背景混合,因此根据背景颜色看起来会有所不同。在这种情况下,alpha
似乎默认为0.75
。由于这也具有类似于饱和度的效果,histplot
不使用饱和度。当在同一个子图中绘制多个直方图时,透明度特别有用。
要获得“真实”颜色,请设置saturation
of the countplot
和alpha
of the histplot
to 1:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(rc={'figure.figsize': (20, 10)}, font_scale=2)
plt.rcParams['axes.grid'] = False
titanic = sns.load_dataset('titanic')
fig, ax = plt.subplots(1, 2)
sns.countplot(x="class", data=titanic, ax=ax[0], color='#5975a4', saturation=1)
sns.histplot(x="who", data=titanic, ax=ax[1], color='#5975a4', alpha=1)
plt.show()
![sns.histplot and sns.countplot with "full" color](https://i.stack.imgur.com/cCjUZ.png)
PS:默认情况下,计数图仅使用 80% 的宽度,而直方图则使用整个宽度。如果需要,可以缩小直方图条形,例如sns.histplot(..., shrink=0.8)
,获得与countplot
.