我有以下数据集:
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()
但即使在这里我也无法真正将它们分开,它只会产生颜色比例。
最佳答案
你对 seaborn 着色的理解有点错误:)
Seaborn 的配色方案是重复的。例如,让我们使用这个调色板:
sns.diverging_palette(220, 20, n=20, center = "dark")
我们将得到这个情节:
如果你不想重复效果,你应该为每个条指定一个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
)
所以你会得到你需要的:
关于python - 用两种颜色分离seaborn barplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754319/