分离两种颜色的海洋条形图

时间:2019-04-18 22:13:04

标签: 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

1 个答案:

答案 0 :(得分:0)

您对Seaborn的着色有点误解:)

Seaborn的配色方案正在重复。例如,让我们使用以下调色板:

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

我们将获得此图:

enter image description here

如果您不想重复效果,则应为每个小节指定一个hue(如果不这样做,则y将用作hue):

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