Hands On Matplotlib?
Hands On Matplotlib?
Line Plot
temperature = [36.6,37,37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
plt.plot(days,temperature)
# plt.axis([0,30,0,50])
plt.title('France Temperature')
plt.xlabel('Days')
plt.ylabel('Temperature')
plt.show()
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6,37,37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
style.use('ggplot')
plt.xlabel('Days',fontsize = 12)
plt.ylabel('Temperature',fontsize = 12)
plt.legend(['Temp Line'],loc = 0)
# plt.grid()
plt.show()
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6,37,37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
style.use('ggplot')
plt.xlabel('Days',fontsize = 12)
plt.ylabel('Temperature',fontsize = 12)
plt.legend(['Temp Line'],loc = 0)
plt.show()
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
france_temperature = [36.6,37,37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,4
japan_temperature = [39,39.4,40,40.7,41,42.5,43.5,44,44.9,44,45,45.1,46,47,46]
style.use('ggplot')
plt.figure(figsize = (15,9))
# plt.legend(['Temp Line'],loc = 0)
plt.legend(loc = 0)
plt.show()
In [6]: x = [1,2,3,4,5,6,7,8]
y = [1,2,3,4,3,2,9,12]
fig, ax = plt.subplots()
ax.plot(x,y,'k')
ax.figesize = (20,5)
# ax.grid(b = True)
# ax = plt.gca()
# ax.set_facecolor()
ax.margins(0)
plt.show()
import random
distance = range(10)
fig, ax = plt.subplots()
# format axes
ax.grid()
ax.margins(0)
ax.set_ylabel('elevation (m)')
ax.set_xlabel('position')
xmax = max(distance)
# display graph
plt.show()
Histogram
import numpy as np
import random
py_student_age = np.random.randint(15,40,(100))
print(ml_student_age)
print(py_student_age)
[20 34 27 28 28 33 39 38 40 44 32 35 41 37 33 36 26 26 24 18 29 42 24 25
28 41 36 26 36 29 21 35 22 29 32 38 36 38 37 26 32 26 22 23 35 41 26 27
32 20 36 38 27 29 23 21 18 21 20 29 44 18 36 28 27 29 42 21 31 28 18 21
26 25 43 19 43 20 40 36 34 30 28 30 30 18 21 40 43 31 28 20 37 25 26 29
26 40 21 19]
[35 36 15 29 32 24 23 33 38 27 15 37 33 16 36 35 24 36 23 21 16 22 29 21
33 18 34 17 33 19 39 31 27 36 35 19 38 29 28 34 22 38 32 38 37 17 22 23
19 31 39 35 16 30 27 24 21 30 29 22 19 18 22 25 27 16 30 17 16 21 24 36
19 29 22 36 36 31 32 22 16 25 16 20 27 36 38 24 24 23 27 16 27 38 27 27
38 28 35 17]
plt.hist(ml_student_age,color = 'pink')
plt.legend(['ML Students'],loc = 0)
plt.show()
plt.show()
plt.show()
plt.show()
plt.show()
# plt.hist(ml_student_age,bins,color = 'green')
# plt.show()
plt.legend(loc = 0)
plt.show()
plt.legend(loc = 0)
plt.show()
plt.legend(loc = 0)
plt.show()
plt.legend(loc = 0)
plt.show()
plt.legend(loc = 0)
plt.show()
plt.legend(loc = 0)
plt.show()
plt.legend(loc = 0)
plt.show()
In [23]: ax = plt.axes()
# plt.figure(figsize = (15,9))
# plt.figure(facecolor = 'r')
ax.set_facecolor('violet')
plt.legend(loc = 0)
plt.show()
In [24]: ax = plt.axes()
# plt.figure(figsize = (15,9))
ax.set_facecolor('black')
plt.legend(loc = 0)
plt.show()
ax = plt.axes()
# plt.figure(figsize = (15,9))
# plt.figure(facecolor = 'r')
ax.set_facecolor('violet')
plt.legend(loc = 0)
plt.show()
Bar Chart
import numpy as np
class1_students = [30,10,20,25,10]
class2_students = [40,5,20,20,10]
class3_students = [35,5,30,15,15]
class4_students = [25,5,35,20,15]
In [28]: plt.bar(classes,class1_students)
In [31]: style.use('ggplot')
plt.figure(figsize = (12,9))
plt.xlabel('Classes',fontsize = 15)
plt.legend(loc = 0)
plt.show()
classes_index = np.arange(len(classes))
width = 0.2
plt.legend(loc = 0)
plt.show()
classes_index = np.arange(len(classes))
width = 0.2
plt.legend(loc = 0)
plt.show()
classes_index = np.arange(len(classes))
width = 0.2
plt.legend(loc = 0)
plt.show()
classes_index = np.arange(len(classes))
width = 0.2
plt.legend(loc = 0)
plt.show()
classes_index = np.arange(len(classes))
width = 0.2
plt.legend(loc = 0)
plt.show()
Scatter Plot
In [37]: import matplotlib.pyplot as plt
import pandas as pd
(1000, 13)
Out[38]:
In [39]: df_google_plat_store_apps.head(5)
Out[39]: Content
App Category Rating Reviews Size Installs Type Price
Rating
Photo
Editor &
Candy
0 ART_AND_DESIGN 4.1 159 19M 10,000+ Free 0 Everyone Art &
Camera &
Grid &
ScrapBook
Coloring
1 book ART_AND_DESIGN 3.9 967 14M 500,000+ Free 0 Everyone Design;
moana
U
Launcher
Lite –
2 FREE Live ART_AND_DESIGN 4.7 87510 8.7M 5,000,000+ Free 0 Everyone Art &
Cool
Themes,
Hide ...
Sketch -
3 Draw & ART_AND_DESIGN 4.5 215644 25M 50,000,000+ Free 0 Teen Art &
Paint
Pixel Draw
- Number
4 Art ART_AND_DESIGN 4.3 967 2.8M 100,000+ Free 0 Everyone
Design;Cr
Coloring
Book
In [40]: x = df_google_plat_store_apps['Rating']
y = df_google_plat_store_apps['Reviews']
plt.scatter(x,y)
<matplotlib.collections.PathCollection at 0x28030a34850>
Out[40]:
In [41]: x = df_google_plat_store_apps['Rating']
y = df_google_plat_store_apps['Reviews']
plt.xlabel('Rating')
plt.ylabel('Reviews')
plt.show()
x = df_google_plat_store_apps['Rating']
y = df_google_plat_store_apps['Reviews']
plt.xlabel('Rating')
plt.ylabel('Reviews')
plt.show()
x = df_google_plat_store_apps['Rating']
y = df_google_plat_store_apps['Reviews']
plt.xlabel('Rating')
plt.ylabel('Reviews')
plt.show()
# x = df_google_plat_store_apps['Rating']
# y = df_google_plat_store_apps['Reviews']
# plt.xlabel('Rating')
# plt.ylabel('Reviews')
# plt.show()
x = df_google_plat_store_apps['Rating']
y = df_google_plat_store_apps['Reviews']
plt.xlabel('Rating')
plt.show()
x = df_google_plat_store_apps['Rating']
y = df_google_plat_store_apps['Reviews']
plt.xlabel('Rating')
plt.show()
In [47]: ax = plt.axes()
ax.set_facecolor('violet')
# plt.figure(figsize = (15,9))
plt.figure(facecolor = 'm')
x = df_google_plat_store_apps['Rating']
y = df_google_plat_store_apps['Reviews']
plt.xlabel('Rating',color = 'white')
plt.show()
import matplotlib.cm as cm
N = 256
radius = np.linspace(.5,1., N)
X = radius * np.cos(angle)
Y = radius * np.sin(angle)
plt.figure(figsize = (15,9))
plt.show()
Pie Chart
In [49]:
plt.pie(class1_students,labels = classes)
plt.show()
class1_students = [45,15,35,25,30]
explode = [0.2,0,0.1,0,0]
textprops = {"fontsize":11}
plt.figure(figsize = (5,5))
# colors = ['']
# class1_students = [45,15,35,25,30]
# explode = [0.2,0,0.1,0,0]
# textprops = {"fontsize":11}
# plt.figure(figsize = (5,5))
# # colors = ['']
# plt.figure(figsize=(7,4))
# #plt.figure(figsize=(16,9)
# colors = ['r','w','r','w','r','w','r','w','r','w','r','w','r','w','r','w','r
# labels = np.ones(20)
# #labels = [1.0,1.0,1.0,1.0,1.0,.........,1.0]
# plt.show()
Subplot
a1 = fig.add_subplot(331)
a2 = fig.add_subplot(332)
a3 = fig.add_subplot(333)
a4 = fig.add_subplot(334)
a5 = fig.add_subplot(335)
a6 = fig.add_subplot(336)
a7 = fig.add_subplot(337)
a8 = fig.add_subplot(338)
a9 = fig.add_subplot(339)
a1 = fig.add_subplot(521)
a2 = fig.add_subplot(522)
a3 = fig.add_subplot(523)
a4 = fig.add_subplot(524)
a5 = fig.add_subplot(525)
a6 = fig.add_subplot(526)
a7 = fig.add_subplot(527)
a8 = fig.add_subplot(528)
a9 = fig.add_subplot(529)
a10 = fig.add_subplot(5,2,10)
In [57]: # x = np.array([0,1,2,3])
# y = np.array([3,8,1,10])
# plt.subplot(1,2,1)
# plt.plot(x,y)
# x = np.array([0,1,2,3])
# y = ([10,20,30,40])
#
# plt.subplot(1,2,2)
# plt.plot(x,y)
# plt.show()
In [58]: # x = np.array([0,1,2,3])
# y = np.array([3,8,1,10])
# plt.subplot(2,1,1)
# plt.plot(x,y)
# x = ([0,1,2,3])
# y = ([10,20,30,40])
#
# plt.subplot(2,1,2)
# plt.plot(x,y)
# plt.show()
plt.subplot(2,2,1)
plt.plot(X,Y,color = 'blue')
plt.subplot(2,2,2)
plt.plot(X,Y,color = 'red')
plt.subplot(2,2,3)
plt.plot(X,Y,color = 'green')
plt.subplot(2,2,4)
plt.plot(X,Y,color = 'black')
plt.show()
plt.subplot(2,2,1)
plt.plot(x,y,color = 'blue')
plt.subplot(2,2,2)
plt.plot(x,y,color = 'red')
plt.subplot(2,2,3)
plt.plot(x,y,color = 'green')
plt.subplot(2,2,4)
plt.plot(x,y,color = 'black')
plt.show()
Save Figure
In [61]: # plt.pie([40,30,20])
# plt.savefig('Pie_Chart',dpi = 100,quality = 99,facecolor = 'white')
# plt.show()
In [64]: img
Out[64]:
[0.23529412, 0.18039216, 0.8666667 , 1. ],
...,
...,
...,
...,
...,
...,
...,
In [65]: type(img)
numpy.ndarray
Out[65]:
In [66]: img.shape
(1836, 3264, 4)
Out[66]:
In [67]: img.ndim
3
Out[67]:
plt.figure(figsize = (10,10))
plt.axis("off")
plt.colorbar()
plt.show()
plt.figure(figsize = (10,10))
plt.axis("off")
plt.colorbar()
plt.show()
plt.figure(figsize = (10,10))
plt.axis("off")
plt.colorbar()
plt.show()
plt.subplot(321)
img = mpimg.imread("pjofficial_windows11.png")
img
single_channel = img[:,:,1]
#plt.figure(figsize = (10,10))
plt.axis("off")
plt.colorbar()
plt.show()
plt.figure(figsize = (15,15))
plt.subplot(322)
img1 = mpimg.imread("pjofficial_windows11.png")
img1
single_channel1 = img[:,:,1]
#plt.figure(figsize = (10,10))
plt.axis("off")
plt.colorbar()
plt.show()
plt.figure(figsize = (15,15))
plt.subplot(323)
img2 = mpimg.imread("pjofficial_windows11.png")
img2
single_channel2 = img[:,:,1]
#plt.figure(figsize = (10,10))
plt.axis("off")
plt.colorbar()
plt.show()
plt.show()
CampusX
In [72]: import matplotlib.pyplot as plt
%matplotlib inline
Pie Chart
plt.legend(loc = 2)
plt.show()
Scatter Plot
In [75]: x = [23,45,12,49,97,32,11,44]
y = [45,34,67,87,55,22,89,90]
plt.scatter(x,y,color = 'r')
plt.xlabel('Age')
plt.ylabel('Salary')
plt.show()
2D Plot
online_price = [233,456,770,120,222,444,200,300]
offline_price = [400,300,100,333,444,565,456,899]
chor_bazaar = [100,200,300,400,150,250,450,500]
jio_mart = [100,198,225,319,429,99,25,11]
plt.xlabel('Product')
plt.ylabel('Price')
plt.legend(loc = 0)
plt.show()
Bar Graph
price = [65000,69000,75000,55000,52000,64000,81000]
plt.bar(year,price,color = 'red')
plt.xlabel('Years')
plt.ylabel('Price INR')
plt.show()
Step Plot
In [79]: x = [1,2,3,4,5]
y = [1,2,3,4,5]
plt.show()
Fill Between
x = np.array([1,2,3,4,5])
y = np.array([1,2,3,4,5])
plt.show()
3D Plot
import numpy as np
ax = plt.axes(projection = '3d')
z = np.linspace(0,t,500)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x,y,z,color = 'red')
plt.show()
plt.show()
Thank You