-3

Data on which I am trying to plot a graph

Data on which I am trying to plot a graph

I am trying to plot the graph with ranges mentioned in the table as x values and the count as its y axis, which I am not able to do with bar graph or histogram.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

0

You need to get discrete values where to plot the bars, then you can change the labels to display the range you want. In this case, the mean of your range would make sense.

import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(df['x_ticks'], df['Count'], width=8) #you can play around with the width as you like
ax.set_xticks(df['x_ticks'])
ax.set_xticklabels([f"{tup[0]}-{tup[1]}" for tup in df['Age']]) # f-string for presentation of your x-labels

Plot:

enter image description here

Input data:

df = pd.DataFrame({
    'Age' : [(x,x+10) for x in range(0,90,10)],
    'Count': [64,115,407,154,86,42,16,5,0]
})
df['x_ticks'] = df['Age'].apply(np.mean)
print(df)
        Age  Count  x_ticks
0   (0, 10)     64      5.0
1  (10, 20)    115     15.0
2  (20, 30)    407     25.0
3  (30, 40)    154     35.0
4  (40, 50)     86     45.0
5  (50, 60)     42     55.0
6  (60, 70)     16     65.0
7  (70, 80)      5     75.0
8  (80, 90)      0     85.0
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Rabinzel
  • 7,757
  • 3
  • 10
  • 30