How to use Pyplot





Bar chart : Graphical representation of data through bars.



lets see the code
import matplotlib.pyplot as pl
fig
= pl.figure()
ax
= fig.add_axes([0,0,3,2])
langs
= ['C', 'C++', 'Java', 'Python', 'PHP']
students
= [21,19,28,29,12]
ax
.bar(langs,students)
pl
.show()













The bar( ) function has width argument to set the width of all bars. It has color argument also, to change the color of bars.


Horizontal bar chart:



CODE:

import matplotlib.pyplot as pl
year=['2007','2008','2009','2010']
p=[97,77,58,92]
c=['b','r','m','g']
pl.barh(year,p,color=c)
pl.xlabel("pass%")
pl.ylabel("year")
pl.show()



OUTPUT










MULTIPLE BAR CHARTS:

CODE:


import numpy as np
import matplotlib.pyplot as plt

data = [[6., 20., 50., 20.],
  [4., 23., 48., 18.],
  [6., 21., 32., 19.]]

X = np.arange(4)
plt.bar(X + 0.00, data[0], color = 'g', width = 0.25)
plt.bar(X + 0.25, data[1], color = 'b', width = 0.25)
plt.bar(X + 0.50, data[2], color = 'r', width = 0.25)


plt.show()































About the Author

Sarkun is a dedicated research student at one of India's premier institutions, the Indian Institute of Science Education and Research (IISER). With over three years of experience in the realm of blogging, Sarkun's passion lies at the interse…

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.