Matplotlib For Data Science..
Matplotlib For Data Science..
plt.plot([1,2,1.5,3,2])
plt.show()
plt.plot([2,4,6,8,10],[1,2,1.5,5,3])
plt.show()
x= np.arange (0,100,2)
plt.plot(x, [x1 for x1 in x])
plt.plot(x, [x1**2 for x1 in x])
plt.plot(x, [x1**3 for x1 in x])
plt.xlim ([0,20])
plt.ylim ([0,80])
plt.show()
Style.use
https://matplotlib.org/tutorials/introductory/customizing.html
import matplotlib.style
style.use ('ggplot')
x =[10,12,14,16]
y= [20,30,15,25]
x1= [11,13,15,17]
y1= [32,28,34,24]
plt.ylabel ('Y-Axis')
plt.legend()
x = ['10','12','14','16','18']
y = [15,10,12,18,20]
plt.plot (x,y)
for i in range(len(x)):
Bar Graph
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
Bar Graph
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
# Fc = Face color
# ec = Edge Color
x = ['10','12','14','16','18']
y = [15,10,12,18,20]
for i in range(len(x)):
Histogram
age = [10,12,45,32,42,22,21,28,35,14,16,20,33,33,35,38,32,42,45,48,18,19,26]
age_group= [0,10,20,30,40,50]
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
age = [10,12,45,32,42,22,21,28,35,14,16,20,33,33,35,38,32,42,45,48,18,19,26]
age_group= [0,10,20,30,40,50]
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
plt.hist (y)
plt.show ()
Scatter plot
x = [1,2,3,4,5,6,7,8,9,10]
y = [18,16,11,15,13,18,13,16,14,18]
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
Area Graph
Example 1
days = [1,2,3,4,5]
sleep = [7,6,4,5,8]
eat = [2,3,4,2,1]
work =[8,9,10,8,9]
play=[4,5,3,3,2]
colors= ['m','r','k','g'])
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
Example 2
days = [1,2,3,4,5]
sleep = [7,6,4,5,8]
eat = [2,3,4,2,1]
work =[8,9,10,8,9]
play=[4,5,3,3,2]
plt.ylabel ('Y-Axis')
plt.legend()
plt.show()
Pie Chart
hours = [5,8,3,4]
activity = ['Sleep','Work','Eat','Play']
col=['g','r','c','y']
plt.pie(hours,labels = activity,colors = col,startangle = 90,shadow = True,
explode = (0,0,0,0.1),autopct = '%1.1f%%')
plt.title ('Pie Chart')
plt.show()
Donut Chart
plt.figure(figsize= (6,6))
hours = [5,8,3,4]
plt.pie (hours)
inner_circle = plt.Circle((0,0), 0.70, fc = 'w')
plt.gca().add_artist(inner_circle)
plt.show
Donut Chart
# library
import matplotlib.pyplot as plt
# create data
names = ['groupA', 'groupB', 'groupC', 'groupD']
size = [12,11,3,30]
Donut Chart
sleep = [7,6,4,5,8]
eat = [2,3,4,2,1]
work =[8,9,10,8,9]
play=[4,5,3,3,2]
plt.figure(figsize= (10,10))
plt.pie(sleep,pctdistance = 0.92,autopct = '%1.1f%%, \n (sleep)', radius = 1.0)
plt.pie(eat,pctdistance = 0.88,autopct = '%1.1f%% \n (eat)', radius = 0.80)
plt.pie(work,pctdistance = 0.85,autopct = '%1.1f%% \n (work', radius = 0.60)
plt.pie(play,pctdistance = 0.80,autopct = '%1.1f%% \n (play)', radius = 0.40)
inner_circle = plt.Circle((0,0), 0.20, fc = 'white')
plt.gca().add_artist(inner_circle)
plt.show()
Multiple Graph
Example 1
x1=np.arange(10,30,2)
y1 = [1,5,6,7,8,3,4,6,5,4]
plt.subplot (211)
plt.plot(x1,y1)
plt.title ('Graph 1')
plt.xlabel ('X-Axis')
plt.ylabel ('Y-Axis')
plt.subplot(212)
plt.plot(x2,y2)
plt.title ('Graph 2')
plt.xlabel ('X-Axis')
plt.ylabel ('Y-Axis')
plt.show()
def f(t):
return (np.exp(-t)*np.cos(2*np.pi*t))
t1=np.arange(10,20,2)
t2= np.arange (11,21,2)
plt.subplot (211)
plt.plot(t1,f(t1),'bo',t2,f(t2))
plt.title ('Graph 1')
plt.xlabel ('X-Axis')
plt.ylabel ('Y-Axis')
plt.subplot(212)
plt.plot(t2,np.cos(2*np.pi*t2))
plt.title ('Graph 2')
plt.xlabel ('X-Axis')
plt.ylabel ('Y-Axis')
plt.show()
Save Graph
x= np.arange (0,100,2)
plt.plot(x, [x1 for x1 in x], label = 'Linear Graph')
plt.plot(x, [x1**2 for x1 in x], label = 'Square Graph')
plt.plot(x, [x1**3 for x1 in x], label = 'Three times of x')
plt.xlim ([0,20])
plt.ylim ([0,80])
plt.xlabel ('X-Axis')
plt.ylabel ('Y-Axis')
plt.legend()
plt.savefig ('Multi Line Graph.png')
plt.show()