python - 用两种颜色分离seaborn barplot

标签 python plot seaborn

我有以下数据集:

words = ['upvoted', 'upvote', 'f***', 'reimer', 'feminists', 'censorship',
       'wet', '0001f914', 'turtle', '0001f602', 'vegans', 'thumbnail',
       'lobby', 'mods', 'removed', 'bitches', 'saffron', 'broadband',
       'hitler', 'ass', 'deleted', 'u', 'tits', 'cheating', 'antifa',
       'iâ', 'â', 'itâ', 'donâ', 'edit', 'thatâ', 'isnâ', 'doesnâ',
       'didnâ', 'canâ', 'youâ', 'theyâ', 'eli5', 'weâ', 'arenâ', 'thereâ',
       'hi', 'wouldnâ', '½ï', 'whatâ', 'ï', '½', 'wasnâ', 'wonâ',
       'eclipse'] 

coefs = [ 1.00157191,  0.95931338,  0.92066619,  0.86347946,  0.83977936,
        0.83912351,  0.83482245,  0.8148754 ,  0.79982483,  0.79402501,
        0.7687297 ,  0.76765479,  0.76096785,  0.75893433,  0.75177131,
        0.74486391,  0.73244163,  0.71302245,  0.70449932,  0.70346844,
        0.69737316,  0.67902944,  0.6746799 ,  0.67338842,  0.6678747 ,
       -2.83723585, -2.82874502, -2.59032368, -2.52985115, -2.1811188 ,
       -1.87094025, -1.66191757, -1.64283967, -1.62282287, -1.61855926,
       -1.55062817, -1.54397537, -1.39775882, -1.3492324 , -1.30638486,
       -1.29859752, -1.14761071, -1.0673105 , -1.06272808, -1.02998239,
       -0.96635257, -0.94262438, -0.91244845, -0.90765028, -0.87274524]

我想制作一个有两种颜色的条形图,一种用于系数的负值,一种用于系数的正值。

我的努力使我:

sns.set(rc={'figure.figsize':(20,10)})
sns.set(font_scale = 1.5)
g = sns.barplot(words,coefs, palette =sns.diverging_palette(220, 20, n=50, center = "dark"))
plt.xticks(rotation=90)
plt.show()

但即使在这里我也无法真正将它们分开,它只会产生颜色比例。

enter image description here

最佳答案

你对 seaborn 着色的理解有点错误:)

Seaborn 的配色方案是重复的。例如,让我们使用这个调色板:

sns.diverging_palette(220, 20, n=20, center = "dark")

我们将得到这个情节:

enter image description here

如果你不想重复效果,你应该为每个条指定一个hue(如果你不这样做,y将被用作色调):

colors = [1 if c >= 0 else 0 for c in coefs]

并将其发送到您的条形图:

g = sns.barplot(
    x=words,
    y=coefs,
    hue=colors, # Here I am!
    palette=sns.color_palette() # Default palette is far better for it
)

所以你会得到你需要的:

enter image description here

关于python - 用两种颜色分离seaborn barplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754319/

相关文章:

python - 获取所有不以字符开头的字符串的最简单方法是什么?

python - Pandas - 根据行值生成唯一 ID

python - 将使用 pandas group by 计算的总和应用于组的所有元素

r - 带有 geom_line 和 geom_ribbon 的图例

python - 如何在 matplotlib.pyplot 中设置 X 和 Y 轴标题

python - 具有来自 DataFrame 行的色调的 Seaborn 分类图

python - 来自无组织数据的条形图 - 数据框创建?

python - Interactive Plot - 改变色调的交互式选项

python - 使用索引列表对 pandas DataFrame 进行索引并填充值

r - 根据值绘制带有条件颜色的折线图