Shiva Prakash
Shiva Prakash

Reputation: 1919

Bar chart of a data in Python

df = pd.DataFrame({'id':['abc1', 'abc2', 'abc3','abc4'], 'age':[21,23,45,34],
'marks':[20, 24, 34, 18]})
 df 

So I want to plot a bar chart with x axis being the age group.
Say I want my groups to be :

10-20, 20-30, 30-40, 40-50

I am new to plotting in Python Can you please help.

Upvotes: 0

Views: 761

Answers (2)

physicalattraction
physicalattraction

Reputation: 6858

I would use the matplotlib library for this.

import matplotlib.pyplot as plt
import numpy as np

def plot_histogram_06():
    data = np.random.normal(loc=30, scale=10, size=[1000, 1])
    bins = (10, 20, 30, 40, 50, 60, 70)
    plt.hist(data, bins=bins)
    plt.savefig('my_plot_06.png')
    plt.close()

Histogram for age

You can add normed=True in the hist argument list if you want to normalize the y-axis. For further options, I refer to the matplotlib manual.

Upvotes: 1

Imran
Imran

Reputation: 13458

import pandas as pd
df = pd.DataFrame({'id':['abc1', 'abc2', 'abc3','abc4'], 'age':[21,23,45,34], 'marks':[20, 24, 34, 18]})
df['age'].hist(bins=[10,20,30,40,50])

enter image description here

Upvotes: 0

Related Questions