python Middle rendering2D Curves need to be used toMatplotlib,Matplotlib It is a Python Of
2D Drawing library, It generates publishing quality level 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 drawings, histogram, Power spectrum, Bar chart, Error graph, Scatter plot, etc..



1. Draw line chart


plt.plot() Can be used to draw line graphs. Only one-dimensional scatter is introduced(n individual)p1 Time, The order of scatter points corresponding to abscissa, from0 reachn-1, The value of vertical coordinate corresponding to scattered point. Example:


# -*- 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 drawing chart objects, Can be created without explicit, Followcv2 Mediumcv2.namedWindow() Similar usage
plt.figure('Draw') plt.plot(p1) # plot Draw line chart plt.draw() # Display drawing plt.pause(5)
# display5 second plt.savefig("easyplot01.jpg") # Save image plt.close() # Close chart


Mapping:



plt.plot() Two dimensional scatterp1,p2(p1 andp2 It's the same length) Time, Abscissax Drawp1, Ordinatey Drawp2, Example:


# -*- 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 drawing chart objects, Can be created without explicit, Followcv2 Mediumcv2.namedWindow() Similar usage plt.figure('Draw')
plt.plot(p1,p2) # plot Draw line chart plt.draw() # Display drawing plt.pause(5) # display5 second
plt.savefig("easyplot01.jpg") # Save image plt.close() # Close chart


Mapping:





2. Draw a scatter diagram


plt.scatter() Used to draw scatter, The incoming parameter must be two-dimensional:plt.scatter(p1,p2), Example:


# -*- 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 drawing chart objects, Can be created without explicit, Followcv2 Mediumcv2.namedWindow() Similar usage plt.figure('Draw')
plt.scatter(p1,p2) # scatter Draw a scatter diagram plt.draw() # Display drawing plt.pause(10) # display10 second
plt.savefig("easyplot.jpg") # Save image plt.close() # Close chart


Mapping:




plt.plot() It can also be used to draw a scatter diagram,plt.plot(p1,p2,'ro') The color of the scatter is red, Shape iso, Example:


# -*- 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 drawing chart objects, Can be created without explicit, Followcv2 Mediumcv2.namedWindow() Similar usage plt.figure('Draw')
plt.plot(p1,p2,'ro') plt.draw() # Display drawing plt.pause(10) # display10 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 drawing chart objects, Can be created without explicit, Followcv2 Mediumcv2.namedWindow() Similar usage 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() # Display drawing plt.pause(10) # display10 second
plt.savefig("easyplot.jpg") # Save image plt.close() # Close chart
Mapping:








4. Draw3D Graph surface graph


plot_surface() Used for rendering3D 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:









Useplot_surface() Draw another3D 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. Draw3D Scatter plot


scatter() Used for rendering3D 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') # Draw data points, The color is red ax.set_zlabel('Z') # Axis of coordinates
ax.set_ylabel('Y') ax.set_xlabel('X') plt.draw() plt.pause(10)
plt.savefig('3D.jpg') plt.close()



Mapping: