0

I have data from different age groups that I am trying to plot as a histogram. I have binned the age groups. When I plot my graph as a bar or line graph my data looks good but when I try to plot as a histogram the graph is wrong. What am I doing wrong?

Binning and saving:

df = pd.read_csv('test.csv')
age_groups = pd.cut(df['Age'], bins=[0, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,np.inf])
Cedar = df.groupby(age_groups)['Cedar'].mean()
Cedar = pd.DataFrame(Cedar, index = None)
Cedar.to_csv('Cedar.csv')

plotting the graph:

Cedar = pd.read_csv('Cedar.csv')
plt.figure();
Cedar.plot(x = 'Age', 
           y = 'Cedar', 
           kind = 'hist', 
           logy = False, 
           figsize = [15,10], 
           fontsize = 15);
rzaratx
  • 756
  • 3
  • 9
  • 29
  • Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) ... [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Nov 05 '20 at 22:50
  • I feel like you want to plot a `barplot` not a `histogram`. Can you try : `Cedar.plot(x = 'Age', y = 'Cedar', kind = 'bar', logy = False, figsize = [15,10], fontsize = 15);` – Grayrigel Nov 06 '20 at 22:56

1 Answers1

1

What you are doing wrong is that you are binning the data. For histogram plots you can give the pure data and set the bins as parameter.

The way you did you are making an histogram of the cedar values.

Flavio Moraes
  • 1,331
  • 1
  • 5
  • 16