1

I am trying to create a bar plot using pandas. I have the following code:

import pandas as pd

indexes = ['Strongly agree', 'Agree', 'Neutral', 'Disagree', 'Strongly disagree']

df = pd.DataFrame({'Q7': [10, 11, 1, 0, 0]}, index=indexes)
df.plot.bar(indexes, df['Q7'].values)

By my reckoning this should work but I get a weird KeyError: 'Strongly agree' thrown at me. I can't figure out why this won't work.

K G
  • 1,715
  • 6
  • 21
  • 29

1 Answers1

2

By invoking plot as a Pandas method, you're referring to the data structures of df to make your plot.

The way you have it set up, with index=indexes, your bar plot's x values are stored in df.index. That's why Wen's suggestion in the comments to just use df.plot.bar() will work, as Pandas automatically looks to use df.index as the x-axis in this case.

Alternately, you can specify column names for x and y. In this case, you can move indexes into a column with reset_index() and then call the new index column explicitly:

df.reset_index().plot.bar(x="index", y="Q7")

Either approach will yield the correct plot:

plot

andrew_reece
  • 20,390
  • 3
  • 33
  • 58
  • Ok thanks. And if I want to colour each bar differently? – K G Dec 13 '17 at 05:18
  • Pandas isn't designed to support multiple colors for a single column. (And in general it's often unnecessary, and even confusing, to encode color as a redundant dimension for a categorical axis.) Still, there's a workaround here, if you really need it: https://stackoverflow.com/a/44495136/2799941. – andrew_reece Dec 13 '17 at 05:32