Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 制作一个聚集的条形图,熊猫_Python_Pandas - Fatal编程技术网

Python 制作一个聚集的条形图,熊猫

Python 制作一个聚集的条形图,熊猫,python,pandas,Python,Pandas,我有一个小熊猫数据框,看起来像这样: Word Percentage1 Percentage2 1 drink 18.166654 29.014272 2 cherry 13.498262 12.802642 3 berry 9.810123 6.775552 4 plum 7.964429 7.105845 5 crisp 7.892941 4.715009 ... 50 spices 0.856775

我有一个小熊猫数据框,看起来像这样:

    Word   Percentage1  Percentage2
1   drink   18.166654   29.014272
2   cherry  13.498262   12.802642
3   berry   9.810123    6.775552
4   plum    7.964429    7.105845
5   crisp   7.892941    4.715009
...
50  spices  0.856775    1.663586
单词(50)和两列数字对应的每一个表示该单词的出现。 如何制作一个聚类图来显示每个单词的两个数字的比较?
我几乎已经尝试了这个网站上提供给其他人的每一段代码,我只是不知道如何将这两个“百分比”列分组。

如果我理解正确,您可以这样做:

df.plot(x="Word", y=["Percentage1", "Percentage2"], kind="bar")
试试这个

df.set_index('Word').plot(kind='bar')
O/p

如果您不想对df中的所有值列执行图表,请使用此选项。只需将索引设置为
X
,所有列的其余部分都设置为
y

输入:

     Word  Percentage1  Percentage2  Percentage3  Percentage4
0   drink    18.166654    29.014272     7.105845    29.014272
1  cherry    13.498262    12.802642     4.715009    12.802642
2   berry     9.810123     6.775552     6.097997     3.408988
3    plum     7.964429     7.105845    12.802642    19.620618
4   crisp     7.892941     4.715009     6.775552    35.832248
O/p


您可以选择堆叠条形图:

# Given
df = pd.DataFrame({'word':['Alpha', 'Bravo', 'Charlie'],
                  'Percentage 1':[10, 3, 0],
                  'Percentage 2': [5, 6, 4]})

df.set_index('word').plot(kind='barh', stacked=True)