Making a clustered bar chart
Asked Answered
A

4

7

I have a little pandas dataframe that looks like this:

import pandas as pd

data = {'Word': ['drink', 'cherry', 'berry', 'plum', 'crisp', 'spices'],
        'Percentage1': [18.166654, 13.498262, 9.810123, 7.964429, 7.892941, 0.856775],
        'Percentage2': [29.014272, 12.802642, 6.775552, 7.105845, 4.715009, 1.663586]}

df = pd.DataFrame(data)

     Word  Percentage1  Percentage2
0   drink    18.166654    29.014272
1  cherry    13.498262    12.802642
2   berry     9.810123     6.775552
3    plum     7.964429     7.105845
4   crisp     7.892941     4.715009
5  spices     0.856775     1.663586

Words (50) and the two columns with figures corresponding to every one of them signifying the incidence of the word. How do I make a clustered chart to show the comparison of the two figures for each word? I have tried virtually every piece of code that was offered to other people on this site, I just don't understand how to group the two "Percentage" columns.

Ammadas answered 4/12, 2018 at 10:43 Comment(0)
B
5

If I understand you correctly, you can do in this way:

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

enter image description here

Bellyache answered 4/12, 2018 at 10:49 Comment(0)
B
6

try this,

df.set_index('Word').plot(kind='bar')

O/P

enter image description here

If you don't want to perform chart for all the values columns in df use this. Just setting index act as X and rest of all the columns act as y

Input:

     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

enter image description here

Beeeater answered 4/12, 2018 at 10:50 Comment(0)
B
5

If I understand you correctly, you can do in this way:

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

enter image description here

Bellyache answered 4/12, 2018 at 10:49 Comment(0)
O
1

You can choose stacked bar graph:

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

plt

Outpoint answered 4/12, 2018 at 10:50 Comment(0)
M
0
  • All of the existing options use .set_index and / or specify y=. However, the simplest solution is to only specify x= for the column to be on the x-axis.
  • The only reason to specify y= is if there are many columns, and only selected columns are desired for plotting.
  • Tested in python 3.10, pandas 1.4.3, matplotlib 3.5.2
ax = df.plot(kind='bar', x='Word', rot=0)

enter image description here

Monotony answered 15/9, 2022 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.