gpt4 book ai didi

python - seaborn中hue、color、edgecolor、facecolor的使用方法

转载 作者:行者123 更新时间:2023-12-05 02:00:23 24 4
gpt4 key购买 nike

我有一个看起来像这样的数据集:

{'prediction': {5: 'c1',
4: 'c1',
3: 'c1',
2: 'c1',
0: 'c1',
1: 'c1',
7: 'c1',
6: 'c1'},
'variable': {5: 'ft1',
4: 'ft2',
3: 'ft3',
2: 'ft4',
0: 'ft5',
1: 'ft6',
7: 'ft7',
6: 'ft8'},
'value': {5: 0.020915912763961077,
4: 0.020388363414781133,
3: 0.007204373035913109,
2: 0.0035298765062560817,
0: -0.002049702058734183,
1: -0.004283512505036808,
7: -0.01882610282871816,
6: -0.022324439779323434}}

我正在尝试制作一个有效的条形图,如下所示:

sns.barplot(data=x, x='value', y='variable', 
hue='prediction', orient="h")

哪个效果好 enter image description here

但是,我希望条形图是红色的。下面:

sns.barplot(data=x, x='value', y='variable', 
hue='prediction', color='red', orient="h")

结果: enter image description here

为什么会变灰?我试过multiple other colors同样的结果。如何将条形变为橙色?

最佳答案

使用 seaborn v0.11.1

尝试使用facecolor:

sns.barplot(data=x, x='value', y='variable', 
hue='prediction', orient="h", facecolor='red')

输出:

enter image description here

  • color 用于所有元素或渐变调色板的种子,而 edgecolor (ec) 和 facecolor (fc) 指定单独的元素。
    • ecfc 优先于 huecolor
  • colorhue 结合使用,根据渐变为条形着色,但是由于 'prediction' 只有一个值,渐变开始灰色。
import pandas as pd
import seaborn as sns

# new data; note there are different values for prediction
{'prediction': {5: 'c1', 4: 'c3', 3: 'c3', 2: 'c1', 0: 'c1', 1: 'c2', 7: 'c2', 6: 'c2'}, 'variable': {5: 'ft1', 4: 'ft2', 3: 'ft3', 2: 'ft4', 0: 'ft5', 1: 'ft6', 7: 'ft7', 6: 'ft8'}, 'value': {5: 0.020915912763961077, 4: 0.020388363414781133, 3: 0.007204373035913109, 2: 0.0035298765062560817, 0: -0.002049702058734183, 1: -0.004283512505036808, 7: -0.01882610282871816, 6: -0.022324439779323434}}

df = pd.DataFrame(x)

sns.barplot(data=df, x='value', y='variable', hue='prediction', orient="h", color='red', ec='k', dodge=False)

enter image description here

请注意,dodge=False 用于防止条形图沿分类轴移动,这在使用 hue 时会发生。

关于python - seaborn中hue、color、edgecolor、facecolor的使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67422810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com