Matplotlib
Matplotlib
Disclaimer
Content of this presentation is not original and
it has been prepared from various sources for
teaching purpose.
Introduction
Matplotlib is a plotting library for the Python
programming language and its extension
NumPy.
x = np.arange(0, 10)
y=x+1
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.show()
Color
Character Color
'b' Blue
'g' Green
'r' Red
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Plot’s linestyle
linestyle description
x = np.arange(0, 10)
y=x+1
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.show()
plt.plot(x,y)
plt.show()
Subplot
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(2, 1, 1)
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y1,label="student 1")
plt.plot(x,y2,label="student 2")
plt.legend(loc=2)
plt.show()
Legend-loc
Location String Location Code
‘best’ 0
‘upper right’ 1
‘upper left’ 2
‘lower left’ 3
‘lower right’ 4
‘right’ 5
‘center left’ 6
‘center right’ 7
‘lower center’ 8
‘upper center’ 9
‘center’ 10
Ticks
x = np.arange(1,11)
y1 = x + 1
y2 = x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y1,label="student 1")
plt.plot(x,y2,label="student 2")
plt.legend(loc=2)
plt.xticks(np.arange(11))
plt.yticks(np.arange(16))
plt.show()
Ticks – User Defined
x = np.arange(1,11)
y1 = x + 1
y2 = x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y1,label="student 1")
plt.plot(x,y2,label="student 2")
plt.legend(loc=2)
plt.xticks(np.arange(1,11),
['a','b','c','d','e','f','g','h','i','j'],
rotation=45)
plt.yticks(np.arange(16))
plt.show()
Scatter Plot with Plot
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0,11)
y=x+1
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.show()
Scatter Plot with plt.scatter
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0,11)
y=x+1
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.show()
Scatter Plot with plt.scatter
x = np.arange(1,11)
y1 = x + 1
y2 = x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.legend(loc=2)
plt.yticks(np.arange(16))
plt.show()
Scatter Plot with plt.scatter
x = np.arange(1,11)
y1 = x + 1
y2 = x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.legend(loc=2)
plt.xticks(np.arange(1, 11), ['a','b','c','d','e','f','g','h','i','j'],
rotation=45)
plt.yticks(np.arange(16))
plt.show()
Scatter Plot and Line Plot with
Plot
x = np.arange(1,11)
y1 = x + 1
y2 = x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.xticks(np.arange(1,11),['a','b','c','d','e','f','g','h','i','j'], rotation=45)
plt.yticks(np.arange(16))
plt.show()
Bar Graph
from matplotlib import pyplot as plt
x = [5, 8, 10]
y = [12, 16, 6]
x2 = [6, 9, 11]
y2 = [6, 15, 7]
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
Histogram
from matplotlib import pyplot as plt
import numpy as np
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.title("histogram")
plt.show()
OpenCV - Installing and
Importing
pip install opencv-python
import cv2
OpenCV - Reading an Image
img = cv2.imread(“Desert.jpg”)
OpenCV - Showing an Image
cv2.imshow(“First Figure”,img)
OpenCV – Writing an Image
cv2.imwrite(“Desert1.jpg”, img)
OpenCV – Resizing an Image
img1=cv2.resize(img, (360, 512))
or
data=pandas.read_csv('temp.csv',header=None)
#default header argument is infer
data=data.as_matrix() # or data=data.values
print(data)
Writing data to a CSV File
import numpy
import pandas as pd
df = pd.DataFrame(a)
df.to_csv("file.csv", header=None, index=False)
Disclaimer
Content of this presentation is not original and
it has been prepared from various sources for
teaching purpose.