Machine Learning Tutorial – Matplotlib, Bar, Graph, Pie Chart, Histogram

-Matlplotlib library was initially developed by john D.Hunter in the year 2003 and keep releasing the updated versions at regular intervals.


1.Basic ploting
     import matplotlib.pyplots as plt
    plt.plot[[2,4,5,6],[3,5,6,2]]
    plt.show()


2.Legends,Labels and Colors
 x1=([2,4,5,3,6])
 y1=([4,6,7,2,1])
 x2=([9,4,6,7,6])
 y2=([4,6,5,1,0])

 plt.plot(x1,y1,linestyle='--',c='r',label='Line 1')
 plt.plot(x2,y2,linestyle='-',c='g',label='Line 2')

 plt.legend()
 plt.title("Matplotlib Demo")

 plt.xlabel("x-value")
 plt.ylabel("y-value")
 plt.xticks([2,4,5,3,6])
 plt.yticks([4,6,7,2,1]) plt.show()
 plt.savefig("plots.img")

 3.Bar plot and histogram

     import numpy as np
        plt.bar([12,43,55,67],[1,.4,.5,.5,1.0])
        plt.xticks(np.arange(11,20))
        plt.title('Bar graph Demo')
        plt.show()
        plt.savefig("bargraph.png")

 4.Histogram 

        IQ=[65,34,64,245,454,65,56,46,3,1,3,6,2,3,5,4,5]
        Age=[4,6,2,4,6,2,6,3,57,5,6]

        plt.hist(IQ,Age,histtype='bar',rwidth=0.8)
        plt.xlabel("IQ range")
        plt.ylabel('No. of students ')

5.Scatter plots 
     import numpy as np 
            x=np.random.randn(200)
            y=np.random.randn(200)

            plt.scatter(x,y,c='r')
            plt.xlabel('x')
            plt.ylabel('y')
            plt.title("Scatter plot demo")
            plt.show()

6.Pie Chart 
     Prog_Lang=['c','C++','java','Python','Ruby','vb']
     usage=[10,20,50,80,20,30,40,]
     plt.axis("equal")
     plt.pie(usage,labels=Prog_Lang,shadow=True)
     plt.title("Pie chart Demo")
     plt.show()

7.Sub polts
     import numpy as np 
        fig=plt.figure()

        ax1=fig.add_subplot(2,2,1)
        ax1.plot(np.random.randn(50),c='red')

        ax2=fig.add_subplot(2,2,2)
        ax2.plot(np.random.randn(50),c='green')

Code :

#Bar plots and Histogram

import matplotlib.pyplot as plt
import numpy as np
plt.bar([1,4,5,6,4],[1,.4,.5,.5,1.0])
plt.xticks(np.arange(0,10))
plt.title(‘Bar graph Demo’)
IQ=[5,6,3,5,4,5,2,8]
Age=[4,6,2,5,6,7,3]
plt.hist(IQ,Age,histtype=’bar’,rwidth=0.8)
plt.show()
plt.xlabel(“IQ range”)
plt.ylabel(‘No. of students ‘)

Code :

import matplotlib.pyplot as plt
plt.plot([2,4,5,6],[3,5,6,2])
plt.show()
x1=([2,4,5,3,6])
y1=([4,6,7,2,1])
x2=([9,4,6,7,6])
y2=([4,6,5,1,0])

plt.plot(x1,y1,linestyle=’–‘,c=’r’,label=’Line 1′)
plt.plot(x2,y2,linestyle=’-‘,c=’g’,label=’Line 2′)

plt.legend()
plt.title(“Matplotlib Demo”)

plt.xlabel(“x-Time”)
plt.ylabel(“y-Money”)
plt.xticks([2,4,5,3,6])
plt.yticks([4,6,7,2,1])
plt.show()

Output :

This image has an empty alt attribute; its file name is Pie-4.jpg
This image has an empty alt attribute; its file name is Pie-5.jpg

Code :

Scatter plot demo

import numpy as np
import matplotlib.pyplot as plt
x=np.random.randn(20)
y=np.random.randn(20)
plt.scatter(x,y,c=’m’)
plt.xlabel(‘x’)
plt.ylabel(‘y’)
plt.title(“Scatter plot demo”)
plt.show()

Pie chart Demo

Prog_Lang=[‘c’,’C++’,’java’,’Python’,’Ruby’,’vb’,’c#’]
usage=[10,20,50,80,20,30,40]
plt.axis(“equal”)
plt.pie(usage,labels=Prog_Lang,shadow=True)
plt.title(“Pie chart Demo”)
plt.show()

fig=plt.figure()
ax1=fig.add_subplot(1,2,1)
ax1.plot(np.random.randn(50),c=’red’)
ax2=fig.add_subplot(2,2,2)
ax2.plot(np.random.randn(50),c=’green’)
ax3=fig.add_subplot(2,2,3)
ax3.plot(np.random.randn(50),c=’black’)
ax4=fig.add_subplot(2,2,4)
ax4.plot(np.random.randn(50),c=’cyan’)

Output :

This image has an empty alt attribute; its file name is Pi-1.jpg
This image has an empty alt attribute; its file name is Pie-2.jpg
This image has an empty alt attribute; its file name is Pie-3.jpg
Graph Plotting

For more details related to python click here

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top