every script matplotlib should import the library like below
import matplotlib.pyplot as plt
import numpy as npDraw first line [2.1.]
To draw a simple plot in matplotlib you can use just two line of code as shown below, Fig. 1 show the output of the code
# check ex1.py
plt.plot([25, 28, 30, 44])
plt.show()As we see in the fig. 1. the Xaxis start from zero and the Yaxis start from 25, as we see in previous code we pass to plt.plot() a just one list [25, 28, 30, 44] so by default matplotlib will assumes it's value of y.
Formatting the style of your plot [2.2.]
# check ex2.py
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()Plotting with keyword strings [2.1.]
we can use simply matplotlib with pandas as we see below
# check ex3.py
# ....
data = {
'a': np.arange(50),
'c': np.random.randint(0, 50, 50),
'd': np.random.randn(50)
}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100
# ...
plt.scatter('a', 'b', c='c', s='d', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()Plotting with categorical variables [2.1.]
matplotlib give user ability to plot using categorical data.
# check ex4.py
# ...
names = ['group_a', 'group_b', 'group_c', 'group_d']
values = [1., 10., -5., 0.]
# ...
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()plt.subplot(nrows, ncols, index)
- nrows : number of rows.
- ncols : number of cols.
- index : where to set the fig. ranges from 1 to numrows*numcols.
# check ex5.py
plt.subplot(1,2,1)
plt.title('121')
plt.subplot(1,2,2)
plt.title('122')
plt.show()# ex6.
plt.text(1, -1.5, r'$\mu=100,\ \sigma=15$')





