0% found this document useful (0 votes)
7 views3 pages

Notes 55 Creating Bar Graph

dfghj

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Notes 55 Creating Bar Graph

dfghj

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating Bar Graph

We can create bar graph with the help of bar() function:


Example:

import matplotlib.pyplot as pl
a=[1,2,3,4,5]
b=[10,25,16,70,45]
pl.bar(a,b)

Example2:

import matplotlib.pyplot as pl
a=[1,2,3,4,5]
b=[10,25,16,70,45]
pl.bar(a,b, color=['r','g','b','y','c'])

Setting width of the bar graph:


import matplotlib.pyplot as pl
a=[1,2,3,4,5]
b=[10,25,16,70,45]
pl.bar(a,b, width=0.2)
pl.show()
pl.bar(a,b, width=0.5)

Changing color of the bars of the bar chart


 we can specify a different color for all the bars of a bar chart
import matplotlib.pyplot as pl
a=[1,2,3,4,5]
b=[10,25,16,70,45]
pl.bar(a,b, color='g')
 we can specify different color for different bar of a bar chart.
import matplotlib.pyplot as pl
a=[1,2,3,4,5]
b=[10,25,16,70,45]
pl.bar(a,b, color=['r','g','b','y','c'])

Creating Multiple Bar Charts

import matplotlib.pyplot as pl
import numpy as np
a=np.arange(5)
b=[10,25,16,70,45]
c=[5,10,12,60,35]
pl.bar(a,b, color='g',width=.25)
pl.bar(a+.25,c,color='r',width=.25)

Steps:
1) Decide number of X points
2) Decide Thickness of each bar
3) Give different color to different data range
4) Width argument remains the same for all ranges being plotted
5) Plot using bar() for each range separately.

Important:
If you will not take care of X points and width of each bar, then the chart will be
created as follows:

import matplotlib.pyplot as pl
import numpy as np
a=np.arange(5)
b=[10,25,16,70,45]
c=[5,10,12,60,35]
pl.bar(a,b, color='g',width=.5)
pl.bar(a,c,color='r',width=.25)

import matplotlib.pyplot as pl
import numpy as np
a=np.arange(5)
b=[10,25,16,70,45]
c=[5,10,12,60,35]
pl.bar(a,b, color='g',width=.25)
pl.bar(a,c,color='r',width=.25)

You might also like