我有一个如下所示的数据框:
我想使用 barplot 绘制这些值,并使用索引作为色调。我怎样才能做到这一点?它可以是 matplotlib 或 seaborn 或任何工具,但我更喜欢前两个。
最佳答案
使用:
df = df.melt(id_vars='index')
sns.barplot(x = 'variable', y = 'value' , data = df, hue = 'index' )
输出:
注意:如果您想在每个栏的顶部添加值,请使用:
plt.figure(figsize = (20,8))
ax = sns.barplot(x = 'variable', y = 'value' , data = df, hue = 'index' )
for p in ax.patches:
height = p.get_height()
ax.text(p.get_x()+p.get_width()/2., height + 300, int(height) , ha="center", fontsize= 'small')
输出:
关于Python Seaborn : how to plot all columns and use index as hue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67859520/