Python Seaborn : how to plot all columns and use index as hue?

标签 python seaborn

我有一个如下所示的数据框:

<表类="s-表"> <头> 索引 9 1 8 3 7 6 2 5 0 4 <正文> 0 32941 3545 2829 2423 1945 1834 1213 1205 1096 969 1 24352 2738 2666 2432 1388 7937 682 3539 2705 1561 2 2137 1271 2401 540 3906 1446 3432 24855 1885 8127

我想使用 barplot 绘制这些值,并使用索引作为色调。我怎样才能做到这一点?它可以是 matplotlib 或 seaborn 或任何工具,但我更喜欢前两个。

最佳答案

使用:

df = df.melt(id_vars='index')
sns.barplot(x = 'variable', y = 'value' , data = df, hue = 'index' )

输出:

enter image description here

注意:如果您想在每个栏的顶部添加值,请使用:


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')

输出:

enter image description here

关于Python Seaborn : how to plot all columns and use index as hue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67859520/

相关文章:

python - 叠加来自多个函数调用的 matplotlib 图,同时也单独绘制它们

python-2.7 - Seaborn 水平条形图

matplotlib - 如何使用不平衡的分类数据缩放seaborn联合图的边际kdeplot

python - numpy,如何找到二维数组中的总行数和一维数组中的总列数

python - 如何区分 Pandas 中除一列之外的两个数据框?

python - 条形图的 Bokeh 对数刻度

python - 同一台机器上有多个 Python 版本?

python - Pandas 将 groupby sum 值分配给原始表中的最后一行

python - Seaborn 不使用 %matplotlib 笔记本显示图例

python - 将 stripplot seaborn 的标记更改为字符串,而不是标记