Basic matplotlib functions
Basic matplotlib functionality
import numpy as np
import matplotlib.pyplot as plt
scatter plot:
x = np.random.normal(5.0, 1.0, 1000) # x array
y = np.random.normal(10.0, 2.0, 1000) # y array
plt.scatter(x, y)
plt.show()
plot 2d funcion: x^4 + 3x^3 - x^2 + 5
x = np.linspace(-1,1, 1000) # from -1 to 1 and 1000 samples total
plt.plot(x, np.power(x,4) + 3 * np.power(x,3) - np.power(x,2) + 5)
plt.show()
mutiple plots using subplots plot four different functions and adjust spacing between them add title for each subplots add title for the plot function1: f(X) = x
function2: f(X) = x^2
function3: f(X) = (x-1)^2
function4: f(X) = x^3
x = np.linspace(-1,1, 1000) # from -1 to 1 and 1000 samples total
fig, a = plt.subplots(2,2,figsize=(10,10))
a[0][0].plot(x,x)
a[0][0].set_title("indetity")
a[0][1].plot(x,x**2)
a[0][1].set_title("x^2")
a[1][0].plot(x,x**2 - 2*x + 1)
a[1][0].set_title("(x-1)^2")
a[1][1].plot(x,x**3)
a[1][1].set_title("x^3")
plt.suptitle('using multiple subplots in a plot')
# plt.subplots_adjust(left=0.1,
# bottom=0.1,
# right=2,
# top=2,
# wspace=0.4,
# hspace=0.4)
plt.subplots_adjust(left=0.1,bottom=0.1, wspace=0.3,hspace=0.3, top=0.8)
plt.show()
plot above functions on same plot using legend
x = np.linspace(-1,1, 1000) # from -1 to 1 and 1000 samples total
plt.plot(x, x , label="x")
plt.plot(x, x**2, label="x^2")
plt.plot(x, x**3, label="x^3")
plt.plot(x, x**2 - 2*x + 1, label="(x-1)^2")
plt.legend(loc="upper right")
plt.show()
bar plots
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
ax.bar(langs,students)
plt.show()
plot histogram of distribution of sepalwidth in iris dataset
import pandas as pd
dataset_path = 'https://datahub.io/machine-learning/iris/r/iris.csv'
dataframe = pd.read_csv(dataset_path)
values = dataframe['sepalwidth'].values
plt.hist(values,bins=100)
plt.show()
plot image
path = 'https://matplotlib.org/stable/_images/sphx_glr_logos2_001_2_0x.png'
from skimage import io
np_image = io.imread(path)
plt.imshow(np_image)
plt.axis('off')
plt.show()