[Data Analysis] Topic 3: Matplotlib

Authored by Tony Feng

Created on May 2nd, 2022

Last Modified on May 2nd, 2022

Intro

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It produces publication-quality figures in a variety of hardcopy formats and interactive environments across platforms.


Basics of Figure Drawing

Plotting

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.linspace(-5,5)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Plot the points using matplotlib
# You can set up many kinds of parameters in plot().
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('sin & cos')
plt.legend(['sin', 'cos'])
plt.show()

Subplots

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.linspace(-10,10)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)        # plt.subplot(211)

# Make the first plot
plt.plot(x, y_sin, color='r', linestyle=':', marker='o')
plt.title('sin')
plt.text(0,0, "Origin") # Display "Origin" near (0,0)
plt.annotate('peak', xy=(np.pi/2, 1), xytext=(3*np.pi/2, 0.5), 
    arrowprops=dict(facecolor='g', shrink=0.05, headlength=10, headwidth=10)) # Show an arrow

# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)    # plt.subplot(212)
plt.plot(x, y_cos, color='b')
plt.title('cos')
plt.grid(True)          # Show grids
plt.show()              # Show the figure.

Common Figures

Histogram

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

matplotlib.rcParams['axes.unicode_minus']=False     # Negative Number
data = np.random.randn(10000)                       # Normal Distribution
plt.hist(data, bins=40, facecolor="blue", edgecolor="black", alpha=0.7)

plt.xlabel("Range")
plt.ylabel("Frequecy")
plt.title("Distribution")
plt.show()

Scatter Plot

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import matplotlib.pyplot as plt
import numpy as np

mu_vec1 = np.array([0,0])
cov_mat1 = np.array([[2,0],[0,2]])

x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100)
x2_samples = np.random.multivariate_normal(mu_vec1+0.2, cov_mat1+0.2, 100)
x3_samples = np.random.multivariate_normal(mu_vec1+0.4, cov_mat1+0.4, 100)

plt.figure(figsize = (8,6))
plt.scatter(x1_samples[:,0],x1_samples[:,1],marker='x',color='blue',alpha=0.6,label='x1')
plt.scatter(x2_samples[:,0],x2_samples[:,1],marker='o',color='red',alpha=0.6,label='x2')
plt.scatter(x3_samples[:,0],x3_samples[:,1],marker='^',color='green',alpha=0.6,label='x3')
plt.legend(loc='best')
plt.show()

Box Plot

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import matplotlib.pyplot as plt
import numpy as np

tang_data = [np.random.normal(0,std,100) for std in range(1,4)]
fig = plt.figure(figsize=(8,6))
plt.boxplot(tang_data,notch=False,sym='s',vert=True)

plt.xticks([y+1 for y in range(len(tang_data))],['x1','x2','x3'])
plt.xlabel('x')
plt.title('box plot')
plt.show()

Pie Chart

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import matplotlib.pyplot as plt
import matplotlib

matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

label_list = ["Part 1", "Part 2", "Part 3"]    
size = [55, 35, 10]    
color = ["red", "green", "blue"]     
explode = [0.05, 0, 0]   

patches, l_text, p_text = plt.pie(size, explode=explode, colors=color, labels=label_list, labeldistance=1.1, autopct="%1.1f%%", shadow=False, startangle=90, pctdistance=0.6)
plt.axis("equal")   # Set the lengths of two axises to be equal
plt.legend()
plt.show()

3D Figure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)

X,Y = np.meshgrid(x,y)

Z = np.sin(np.sqrt(X**2+Y**2))
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow')
ax.contour(X,Y,Z,zdim='z',offset=-2 ,cmap='rainbow')

ax.set_zlim(-2,2)
plt.show()

Reference


MIT License
Last updated on May 02, 2022 09:44 EDT
Built with Hugo
Theme Stack designed by Jimmy