python Drawing in 2D The graph needs to use Matplotlib,Matplotlib It's a Python Of
2D Drawing library , It generates publishing quality graphics in various hard copy formats and cross platform interactive environments , adopt
Matplotlib, Developers can only need a few lines of code , You can generate a drawing , histogram , Power spectrum , Bar chart , Error chart , Scatter diagram, etc .



1. Draw line chart


plt.plot() It can be used to draw line chart . Only one-dimensional scatter is introduced (n individual )p1 Time , The order of scatter points corresponding to abscissa , from 0 reach n-1, The value of scatter point corresponding to ordinate . Examples :


# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np
p1=[0,1.1,1.8,3.1,4.0] # data point # Create a drawing chart object , You can not create it explicitly , Follow cv2 Medium cv2.namedWindow() The usage is similar
plt.figure('Draw') plt.plot(p1) # plot Draw line chart plt.draw() # Show drawing plt.pause(5)
# display 5 second plt.savefig("easyplot01.jpg") # Save image plt.close() # Close chart


mapping :



plt.plot() Two dimensional scatter p1,p2(p1 and p2 The length should be the same ) Time , Abscissa x draw p1, Ordinate y draw p2, Examples :


# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np
p1=[0,1.1,1.8,3.1,4.0] # data point p2=[2,2.4,4.3,3.5,2.5]
# Create a drawing chart object , You can not create it explicitly , Follow cv2 Medium cv2.namedWindow() The usage is similar plt.figure('Draw')
plt.plot(p1,p2) # plot Draw line chart plt.draw() # Show drawing plt.pause(5) # display 5 second
plt.savefig("easyplot01.jpg") # Save image plt.close() # Close chart


mapping :





2. Draw scatter plot


plt.scatter() Used to draw scatter plot , The passed in parameter must be two-dimensional :plt.scatter(p1,p2), Examples :


# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np
p1=[0,1.1,1.8,3.1,4.0] # data point p2=[2,2.4,4.3,3.5,2.5]
# Create a drawing chart object , You can not create it explicitly , Follow cv2 Medium cv2.namedWindow() The usage is similar plt.figure('Draw')
plt.scatter(p1,p2) # scatter Draw scatter plot plt.draw() # Show drawing plt.pause(10) # display 10 second
plt.savefig("easyplot.jpg") # Save image plt.close() # Close chart


mapping :




plt.plot() It can also be used to draw scatter plots ,plt.plot(p1,p2,'ro') The color of the scatter is red , The shape is o, Examples :


# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np
p1=[0,1.1,1.8,3.1,4.0] # data point p2=[2,2.4,4.3,3.5,2.5]
# Create a drawing chart object , You can not create it explicitly , Follow cv2 Medium cv2.namedWindow() The usage is similar plt.figure('Draw')
plt.plot(p1,p2,'ro') plt.draw() # Show drawing plt.pause(10) # display 10 second
plt.savefig("easyplot.jpg") # Save image plt.close() # Close chart



mapping :





3. Add axis description , Add title , Insert text in picture , show grid


Axis description :plt.xlabel(),plt.ylabel()
Add title : plt.title()
Insert text : plt.text(60, .025, r'$number=5, sigma=0$')
show grid : plt.grid(True)





# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np
p1=[0,1.1,1.8,3.1,4.0] # data point p2=[2,2.4,4.3,3.5,2.5]
# Create a drawing chart object , You can not create it explicitly , Follow cv2 Medium cv2.namedWindow() The usage is similar plt.figure('Draw')
plt.plot(p1,p2,'ro') plt.xlabel('number of samples') plt.ylabel('value of
samples') plt.title('sample data') plt.text(1.1, 3.6, r'$number=5, sigma=0$')
plt.grid(True) plt.draw() # Show drawing plt.pause(10) # display 10 second
plt.savefig("easyplot.jpg") # Save image plt.close() # Close chart
mapping :








4. draw 3D Figure surface


plot_surface() For drawing 3D Surface graph :


# -*- coding: utf-8 -*- from matplotlib import pyplot as plt import numpy as
np from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig)
X = [0,1.1,1.8,3.1,4.0] Y = [2,2.4,4.3,3.5,2.5] X, Y = np.meshgrid(X, Y) Z =
[2,2.4,4.3,3.5,2.5] ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='rainbow') plt.draw() plt.pause(10) plt.savefig('3D.jpg') plt.close()
mapping :









use plot_surface() Draw another one 3D curved surface :


# -*- coding: utf-8 -*- from matplotlib import 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)
R = np.sqrt(X**2 + Y**2) Z = X*Y ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='rainbow') plt.draw() plt.pause(10) plt.savefig('3D.jpg') plt.close()


mapping :










5. draw 3D Scatter plot


scatter() For drawing 3D Scatter plot :


# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D x = [0,1.1,1.8,3.1,4.0] y =
[2,2.4,4.3,3.5,2.5] z = [1,2,3,4,5] ax = plt.subplot(projection='3d') #
Create a 3D drawing project ax.scatter(x, y, z, c='r') # Drawing data points , The color is red ax.set_zlabel('Z') # Axis
ax.set_ylabel('Y') ax.set_xlabel('X') plt.draw() plt.pause(10)
plt.savefig('3D.jpg') plt.close()



mapping :