0% found this document useful (0 votes)
57 views

Hands On Matplotlib?

The document contains Python code for creating various line plots, scatter plots, and histograms to visualize and compare temperature data from France and Japan, as well as student age data from machine learning and Python courses. The code explores different visualization options including styling, colors, labels, legends, grids, subplots, and overlays. Multiple plots are created within each code cell to demonstrate different plotting parameters and techniques.

Uploaded by

sai yogesh
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)
57 views

Hands On Matplotlib?

The document contains Python code for creating various line plots, scatter plots, and histograms to visualize and compare temperature data from France and Japan, as well as student age data from machine learning and Python courses. The code explores different visualization options including styling, colors, labels, legends, grids, subplots, and overlays. Multiple plots are created within each code cell to demonstrate different plotting parameters and techniques.

Uploaded by

sai yogesh
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/ 40

In 

[1]: import matplotlib.pyplot as plt

Line Plot

In [2]: 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]

plt.plot(days,temperature)

# plt.axis([0,30,0,50])

plt.title('France Temperature')

plt.xlabel('Days')

plt.ylabel('Temperature')

plt.show()

In [3]: from matplotlib import style

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.plot(days,temperature,color = 'm',marker = 'o',linestyle = '--',linewidth =


# plt.axis([0,30,0,50])

plt.title('France Temperature',fontsize = 14)

plt.xlabel('Days',fontsize = 12)

plt.ylabel('Temperature',fontsize = 12)

plt.legend(['Temp Line'],loc = 0)

# plt.grid()

plt.show()

In [4]: from matplotlib import style

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.plot(days,temperature,color = 'red',marker = 'o',linestyle = '--',linewidth


# plt.axis([0,30,0,50])

plt.title('France Temperature',fontsize = 14)

plt.xlabel('Days',fontsize = 12)

plt.ylabel('Temperature',fontsize = 12)

plt.legend(['Temp Line'],loc = 0)

plt.grid(color = 'green',linestyle = '-',linewidth = 1)

plt.show()

In [5]: from matplotlib import style

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.plot(days,france_temperature,color = 'red',marker = 'o',linestyle = '--',li


plt.plot(days,japan_temperature,color = 'green',marker = 'o',linestyle = '--',l
# plt.axis([0,30,0,50])

plt.title('France & Japan Temperature',fontsize = 14,color = 'blue')

plt.xlabel('Days',fontsize = 12,color = 'm')

plt.ylabel('Temperature',fontsize = 12,color = 'red')

# plt.legend(['Temp Line'],loc = 0)

plt.legend(loc = 0)

plt.grid(color = 'white',linestyle = '-',linewidth = 1)

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)

ax.axhspan(0,4,facecolor = 'green',alpha = 0.5)

ax.axhspan(4,9,facecolor = 'yellow',alpha = 0.5)

ax.axhspan(9,12,facecolor = 'red',alpha = 0.5)

plt.show()

In [7]: import matplotlib.pyplot as plt

import random

# generate random data

elevation = [random.randrange(-y -1, 0) for y in range(10)]

distance = range(10)

# get reference to axes

fig, ax = plt.subplots()

# plot data with a yellow line

ax.plot(distance, elevation, 'y', linewidth=3)

# format axes

ax.grid()

ax.margins(0)

ax.set_ylabel('elevation (m)')

ax.set_xlabel('position')

ax.set_ylim([min(elevation) - 1, max(elevation) + 1])

# get range of axes

ymin, ymax = ax.get_ylim()

xmax = max(distance)

# set background colours

ax.axhspan(ymin, ymax, 0 / xmax, 3 / xmax, facecolor='green')

ax.axhspan(ymin, ymax, 3 / xmax, 8 / xmax, facecolor='brown')

ax.axhspan(ymin, ymax, 8 / xmax, 9 / xmax, facecolor='blue')

# display graph

plt.show()

Histogram

In [8]: import matplotlib.pyplot as plt

import numpy as np

import random

In [9]: ml_student_age = np.random.randint(18,45,(100))

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]

In [10]: plt.figure(figsize = (15,9))

plt.hist(ml_student_age,color = 'pink')

plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(['ML Students'],loc = 0)

plt.show()

In [11]: bins = [15,20,25,30,35,40,45]

plt.hist(ml_student_age,bins,rwidth = 0.8,color = 'blue',histtype = 'step')

plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.show()

In [12]: bins = [15,20,25,30,35,40,45]

plt.hist(ml_student_age,bins,rwidth = 0.8,color = 'y',orientation = 'horizontal


plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.show()

In [13]: bins = [15,20,25,30,35,40,45]

plt.hist(ml_student_age,bins,rwidth = 0.8,color = 'red',orientation = 'horizont


plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.show()

In [14]: bins = [15,20,25,30,35,40,45]

plt.hist(ml_student_age,bins,rwidth = 0.8,color = 'y')

plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.show()

In [15]: # bins = [15,20,25,30,35,40,45]

# plt.hist(ml_student_age,bins,color = 'green')

# plt.title('ML Students Age')

# plt.xlabel('Students Age Cotegory')

# plt.ylabel('Numbers Students Age')

# plt.show()

In [16]: plt.hist(ml_student_age,color = 'red',label = 'ML Students')

plt.hist(py_student_age,color = 'green',label = 'Python Students')

plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [17]: plt.hist(ml_student_age,color = 'red',label = 'ML Students',orientation = 'hori


plt.hist(py_student_age,color = 'green',label = 'Python Students')

plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [18]: plt.hist(ml_student_age,color = 'red',label = 'ML Students',orientation = 'hori


plt.hist(py_student_age,color = 'green',label = 'Python Students',histtype = 'b
plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [19]: plt.hist(ml_student_age,color = 'red',label = 'ML Students',orientation = 'vert


plt.hist(py_student_age,color = 'green',label = 'Python Students',orientation =
plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [20]: plt.figure(figsize = (15,9))

plt.hist([ml_student_age,py_student_age],color = ['m','y'],label = ['ML Student


# plt.hist(py_student_age,color = 'green',label = 'Python Students',orientation
plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [21]: plt.figure(figsize = (15,9))

plt.hist([ml_student_age,py_student_age],color = ['pink','white'],label = ['ML


# plt.hist(py_student_age,color = 'green',label = 'Python Students',orientation
plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [22]: plt.figure(figsize = (15,9))

plt.hist(ml_student_age,color = 'pink',label = 'ML Students')

plt.hist(py_student_age,color = 'white',label = 'Python Students')

plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

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.hist([ml_student_age,py_student_age],color = ['pink','white'],label = ['ML


# plt.hist(py_student_age,color = 'green',label = 'Python Students',orientation
plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [24]: ax = plt.axes()

# plt.figure(figsize = (15,9))

ax.set_facecolor('black')

plt.hist([ml_student_age,py_student_age],color = ['pink','white'],label = ['ML


# plt.hist(py_student_age,color = 'green',label = 'Python Students',orientation
plt.title('ML Students Age')

plt.xlabel('Students Age Cotegory')

plt.ylabel('Numbers Students Age')

plt.legend(loc = 0)

plt.show()

In [25]: plt.figure(facecolor = 'y')

ax = plt.axes()

# plt.figure(figsize = (15,9))

# plt.figure(facecolor = 'r')

ax.set_facecolor('violet')

plt.hist([ml_student_age,py_student_age],color = ['r','g'],label = ['ML Student


# plt.hist(py_student_age,color = 'green',label = 'Python Students',orientation
plt.title('ML Students Age',color = 'white',fontsize = 15)

plt.xlabel('Students Age Cotegory',color = 'black',fontsize = 12)

plt.ylabel('Numbers Students Age',color = 'black',fontsize = 12)

plt.grid(color = 'black',linestyle = '--',linewidth = 1)

plt.legend(loc = 0)

plt.show()

Bar Chart

In [26]: import matplotlib.pyplot as plt

import numpy as np

from matplotlib import style

In [27]: classes = ['Python','R','AI','ML','DS']

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)

<BarContainer object of 5 artists>


Out[28]:
In [29]: plt.barh(classes,class1_students)

<BarContainer object of 5 artists>


Out[29]:

In [30]: plt.bar(classes,class1_students,width = 0.2,align = 'edge',color = 'g',edgecolo

<BarContainer object of 5 artists>


Out[30]:

In [31]: style.use('ggplot')

plt.figure(figsize = (12,9))

plt.bar(classes,class1_students,width = 0.6,align = 'edge',color = 'blue',edgec


plt.title('Students Classes',fontsize = 25,color = 'b')

plt.xlabel('Classes',fontsize = 15)

plt.ylabel('Number Of Students',fontsize = 15)

plt.legend(loc = 0)

plt.show()

In [32]: plt.figure(figsize = (12,9))

classes_index = np.arange(len(classes))

width = 0.2

plt.bar(classes_index,class1_students,width,color = 'b',label = 'Class 1 Studen


plt.bar(classes_index + width,class2_students,width,color = 'g',label = 'Class
plt.xticks(classes_index + width,classes)

plt.title('Students Classes',fontsize = 20,color = 'k')

plt.xlabel('Classes',fontsize = 15,color = 'k')

plt.ylabel('Number Of Students',fontsize = 15,color = 'k')

plt.legend(loc = 0)

plt.show()

In [33]: plt.figure(figsize = (12,9))

classes_index = np.arange(len(classes))

width = 0.2

plt.bar(classes_index,class1_students,width,color = 'b',label = 'Class 1 Studen


plt.bar(classes_index + width,class2_students,width,color = 'g',label = 'Class
plt.bar(classes_index + width + width,class3_students,width,color = 'y',label =
plt.xticks(classes_index + width,classes)

plt.title('Students Classes',fontsize = 20) # ,color = 'k')

plt.xlabel('Classes',fontsize = 15) # ,color = 'k')

plt.ylabel('Number Of Students',fontsize = 15) # ,color = 'k')

plt.legend(loc = 0)

plt.show()

In [34]: plt.figure(figsize = (12,9))

classes_index = np.arange(len(classes))

width = 0.2

plt.bar(classes_index,class1_students,width,color = 'b',label = 'Class 1 Studen


plt.bar(classes_index + width,class2_students,width,color = 'g',label = 'Class
plt.bar(classes_index + width + width,class3_students,width,color = 'y',label =
plt.bar(classes_index + width + width + width,class4_students,width,color = 'r
plt.xticks(classes_index + width,classes)

plt.title('Students Classes',fontsize = 20) # ,color = 'k')

plt.xlabel('Classes',fontsize = 15) # ,color = 'k')

plt.ylabel('Number Of Students',fontsize = 15) # ,color = 'k')

plt.legend(loc = 0)

plt.show()

In [35]: plt.figure(figsize = (15,9))

classes_index = np.arange(len(classes))

width = 0.2

plt.barh(classes_index,class1_students,width,color = 'b',label = 'Class 1 Stude


plt.barh(classes_index + width,class2_students,width,color = 'g',label = 'Class
plt.barh(classes_index + width + width,class3_students,width,color = 'y',label
plt.barh(classes_index + width + width + width,class4_students,width,color = 'r
plt.xticks(classes_index + width,classes)

plt.title('Students Classes',fontsize = 20) # ,color = 'k')

plt.xlabel('Classes',fontsize = 15) # ,color = 'k')

plt.ylabel('Number Of Students',fontsize = 15) # ,color = 'k')

plt.legend(loc = 0)

plt.show()

In [36]: # plt.figure(figsize = (12,9))

classes_index = np.arange(len(classes))

width = 0.2

plt.barh(classes_index,class1_students,width,color = 'b',label = 'Class 1 Stude


plt.barh(classes_index + width,class2_students,width,color = 'g',label = 'Class
plt.barh(classes_index + width + width,class3_students,width,color = 'y',label
plt.xticks(classes_index + width,classes)

plt.title('Students Classes',fontsize = 20) # ,color = 'k')

plt.xlabel('Classes',fontsize = 15) # ,color = 'k')

plt.ylabel('Number Of Students',fontsize = 15) # ,color = 'k')

plt.legend(loc = 0)

plt.show()

Scatter Plot
In [37]: import matplotlib.pyplot as plt

import pandas as pd

In [38]: df_google_plat_store_apps = pd.read_csv('C:\\Users\prasad jadhav\Downloads\Pand


df_google_plat_store_apps.shape

(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.scatter(x,y, color = 'r')

plt.title('Google Play Store Apps',color = 'r')

plt.xlabel('Rating')

plt.ylabel('Reviews')

plt.show()

In [42]: plt.figure(figsize = (15,9))

x = df_google_plat_store_apps['Rating']

y = df_google_plat_store_apps['Reviews']

plt.scatter(x,y, color = 'r',marker = '*',s = 100) # ,alpha = 0.5)

plt.title('Google Play Store Apps',color = 'r')

plt.xlabel('Rating')

plt.ylabel('Reviews')

plt.show()

In [43]: plt.figure(figsize = (15,9))

x = df_google_plat_store_apps['Rating']

y = df_google_plat_store_apps['Reviews']

plt.scatter(x,y, color = 'green',marker = '+',s = 75) # ,linewidths = 10) # ,al


plt.title('Google Play Store Apps',color = 'k')

plt.xlabel('Rating')

plt.ylabel('Reviews')

plt.show()

In [44]: # plt.figure(figsize = (15,9))

# x = df_google_plat_store_apps['Rating']

# y = df_google_plat_store_apps['Reviews']

# plt.scatter(x,y, color = 'r',marker = '+',s = 75,linewidths = 10,alpha = 0.5,


# plt.title('Google Play Store Apps',color = 'k')

# plt.xlabel('Rating')

# plt.ylabel('Reviews')

# plt.show()

In [45]: plt.figure(figsize = (15,9))

x = df_google_plat_store_apps['Rating']

y = df_google_plat_store_apps['Reviews']

# plt.scatter(x,y, color = 'r',marker = '+',s = 75) # ,linewidths = 10,alpha =


plt.scatter(x,y,color = 'g',marker = '+',s = 75)

plt.scatter(x,df_google_plat_store_apps['Installs'],color = 'r',marker = '*',s


plt.title('Google Play Store Apps',color = 'k')

plt.xlabel('Rating')

plt.ylabel('Reviews & Installs')

plt.show()

In [46]: plt.figure(figsize = (15,9))

x = df_google_plat_store_apps['Rating']

y = df_google_plat_store_apps['Reviews']

# plt.scatter(x,y, color = 'r',marker = '+',s = 75) # ,linewidths = 10,alpha =


plt.scatter(x,y,color = 'g',marker = '+',s = 100,linewidths = 10,alpha = 0.5,ed
plt.scatter(x,df_google_plat_store_apps['Installs'],color = 'b',marker = '*',s
plt.title('Google Play Store Apps',color = 'k')

plt.xlabel('Rating')

plt.ylabel('Reviews & Installs')

plt.show()

C:\Users\prasad jadhav\AppData\Local\Temp\ipykernel_17068\89367763.py:5: UserW


arning: You passed a edgecolor/edgecolors ('b') for an unfilled marker ('+').
Matplotlib is ignoring the edgecolor in favor of the facecolor. This behavior
may change in the future.

plt.scatter(x,y,color = 'g',marker = '+',s = 100,linewidths = 10,alpha = 0.


5,edgecolors = 'b')

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.scatter(x,y, color = 'r',marker = '+',s = 75) # ,linewidths = 10,alpha =


plt.scatter(x,y,color = 'green',marker = '+',s = 75) # ,linewidths = 10,alpha =
plt.scatter(x,df_google_plat_store_apps['Installs'],color = 'red',marker = '*'
plt.title('Google Play Store Apps',color = 'white')

plt.xlabel('Rating',color = 'white')

plt.ylabel('Reviews & Installs',color = 'white')

plt.grid(color = 'black',linestyle = '--',linewidth = 1)

plt.show()

In [48]: import numpy as np

import matplotlib.cm as cm

import matplotlib.pyplot as plt

N = 256

angle = np.linspace(0,8 * 2 * np.pi, N)

radius = np.linspace(.5,1., N)

X = radius * np.cos(angle)

Y = radius * np.sin(angle)

plt.figure(figsize = (15,9))

plt.scatter(X,Y, c = angle, cmap = cm.hsv)

plt.show()

Pie Chart

import matplotlib.pyplot as plt

In [49]:

In [50]: classes = ['Python','R','Machine Learning','Artificial Intelligence','Data Scie


class1_students = [45,15,35,25,30]

plt.pie(class1_students,labels = classes)

plt.show()

In [51]: classes = ["Python", "R", "Machine Learning", "Artificial Intelligence", "Data


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.pie(class1_students, labels = classes, explode = explode, autopct = "%0.1f%


# plt.legend(loc = 2)
plt.show()

In [52]: # classes = ["Python", "R", "Machine Learning", "Artificial Intelligence", "Dat


#

# class1_students = [45,15,35,25,30]

# explode = [0.2,0,0.1,0,0]

# textprops = {"fontsize":11}

# plt.figure(figsize = (5,5))

# # colors = ['']

# wedgeprops = {'linewidth': 4,'width': 1,'edgecolor': 'k'}

# plt.pie(class1_students, labels = classes, explode = explode, autopct = "%0.1


# plt.legend(loc = 2)
# plt.show()

In [53]: # import numpy as np

# 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.pie([1], colors="k", radius = 2.05)

# plt.pie(labels, colors=colors, radius = 2.0)

# plt.pie([1], colors="g", radius = 1.8)

# plt.pie([1], colors="y", radius = 1.6)

# plt.pie([1], colors="c", radius = 1.3)

# plt.pie([1], colors="b", radius = 1.1)

# plt.pie([1], colors="m", radius = 0.9)

# plt.pie([1], colors="b", radius = 0.31)

# plt.pie(labels, colors=colors, radius = 0.3)

# plt.pie([1], colors="w", radius = 0.2)

# plt.pie([1], colors="k", radius = 0.1)

# plt.show()

Subplot

In [54]: import numpy as np

import matplotlib.pyplot as plt

In [55]: fig = plt.figure(figsize=(10,8))

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)

In [56]: fig = plt.figure(figsize=(10,8))

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()

In [59]: plt.figure(figsize = (8,8))

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()

In [60]: plt.figure(figsize = (8,8))

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()

Image Show & Color Bar

In [62]: import matplotlib.pyplot as plt

import matplotlib.image as mpimg

In [63]: img = mpimg.imread("pjofficial_windows11.png")

In [64]: img

array([[[0.23529412, 0.18039216, 0.8666667 , 1. ],

Out[64]:
[0.23529412, 0.18039216, 0.8666667 , 1. ],

[0.23529412, 0.18039216, 0.8666667 , 1. ],

...,

[0.21176471, 0.08235294, 0.627451 , 1. ],

[0.21176471, 0.08235294, 0.627451 , 1. ],

[0.21176471, 0.08235294, 0.627451 , 1. ]],

[[0.2 , 0.14117648, 0.85490197, 1. ],

[0.2 , 0.14117648, 0.85490197, 1. ],

[0.2 , 0.14117648, 0.85490197, 1. ],

...,

[0.17254902, 0.04313726, 0.6 , 1. ],

[0.17254902, 0.04313726, 0.6 , 1. ],

[0.17254902, 0.04313726, 0.6 , 1. ]],

[[0.2 , 0.14117648, 0.85882354, 1. ],

[0.2 , 0.14117648, 0.85882354, 1. ],

[0.2 , 0.14117648, 0.85882354, 1. ],

...,

[0.16862746, 0.03529412, 0.60784316, 1. ],

[0.16862746, 0.03529412, 0.60784316, 1. ],

[0.16862746, 0.03529412, 0.60784316, 1. ]],

...,

[[0.04705882, 0.00392157, 0.12156863, 1. ],

[0.04705882, 0.00392157, 0.12156863, 1. ],

[0.04705882, 0.00392157, 0.12156863, 1. ],

...,

[0.08627451, 0.01568628, 0.24313726, 1. ],

[0.08627451, 0.01568628, 0.24313726, 1. ],

[0.08627451, 0.01568628, 0.24313726, 1. ]],

[[0.04705882, 0.00392157, 0.12156863, 1. ],

[0.04705882, 0.00392157, 0.12156863, 1. ],

[0.04705882, 0.00392157, 0.12156863, 1. ],

...,

[0.08235294, 0.01176471, 0.23921569, 1. ],

[0.08235294, 0.01176471, 0.23921569, 1. ],

[0.08235294, 0.01176471, 0.23921569, 1. ]],

[[0.09019608, 0.04705882, 0.16470589, 1. ],

[0.09019608, 0.04705882, 0.16470589, 1. ],

[0.09019608, 0.04705882, 0.16470589, 1. ],

...,

[0.1254902 , 0.05490196, 0.28235295, 1. ],

[0.1254902 , 0.05490196, 0.28235295, 1. ],

[0.1254902 , 0.05490196, 0.28235295, 1. ]]], dtype=float32)

In [65]: type(img)

numpy.ndarray
Out[65]:

In [66]: img.shape

(1836, 3264, 4)
Out[66]:

In [67]: img.ndim

3
Out[67]:

In [68]: single_channel = img[:,:,1]

plt.figure(figsize = (10,10))

plt.axis("off")

plt.imshow(single_channel, cmap = "binary")

plt.colorbar()

plt.show()

In [69]: single_channel1 = img[:,:,1]

plt.figure(figsize = (10,10))

plt.axis("off")

plt.imshow(single_channel1, cmap = "Blues")

plt.colorbar()

plt.show()

In [70]: single_channel2 = img[:,:,1]

plt.figure(figsize = (10,10))

plt.axis("off")

plt.imshow(single_channel2, cmap = "Oranges")

plt.colorbar()

plt.show()

In [71]: plt.figure(figsize = (15,15))

plt.subplot(321)

img = mpimg.imread("pjofficial_windows11.png")

img

single_channel = img[:,:,1]

#plt.figure(figsize = (10,10))

plt.axis("off")

plt.imshow(single_channel, cmap = "binary")

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.imshow(single_channel1, cmap = "Blues")

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.imshow(single_channel2, cmap = "Oranges")

plt.colorbar()

plt.show()

plt.show()

CampusX
In [72]: import matplotlib.pyplot as plt

# from matplotlib import style

%matplotlib inline

Pie Chart

In [73]: areas = ['Marketing','Sale','Development','HR','Customer Support']


budget = [2.5,4,10,1,6]

In [74]: plt.pie(budget,labels = areas,autopct = '%0.2f%%')

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.title('Age & Salary Graph')

plt.xlabel('Age')

plt.ylabel('Salary')

plt.show()

2D Plot

In [76]: product_id = [1,2,3,4,5,6,7,8]

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]

In [77]: plt.plot(product_id,online_price,color = 'red',marker = 'o',markersize = 5,labe


plt.plot(product_id,offline_price,color = 'green',marker = '*',markersize = 5,l
plt.plot(product_id,chor_bazaar,color = 'blue',marker = '+',markersize = 5,labe
plt.plot(product_id,chor_bazaar,color = 'yellow',marker = '^',markersize = 5,la

plt.title('Product Price Comparision',fontsize = 13)

plt.xlabel('Product')

plt.ylabel('Price')

plt.legend(loc = 0)

plt.show()

Bar Graph

In [78]: year = [2012,2013,2014,2015,2016,2017,2018]

price = [65000,69000,75000,55000,52000,64000,81000]

plt.bar(year,price,color = 'red')

plt.title('Year By Year Comparision')

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.step(x,y,color = 'red',marker = 'o')

plt.show()

Fill Between

In [80]: import numpy as np

x = np.array([1,2,3,4,5])

y = np.array([1,2,3,4,5])

plt.plot(x,y,color = 'red',marker = 'o')

plt.fill_between(x,y,color = 'green',where = (x>=2) & (x<=4))

plt.show()

3D Plot

In [1]: import ipywidgets as widgets

from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

In [2]: def theta(t):

fig = plt.figure(figsize = (10,15))

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()

widgets.interact(theta,t = widgets.Play(min = 0,max = 15))

plt.show()

interactive(children=(Play(value=0, description='t', max=15), Output()), _dom_


classes=('widget-interact',))

Thank You

You might also like