# 1. Pyplot basic syntax

Time：2020-1-3

Catalog

• 1. Pyplot basic syntax
• 2. Scatter chart and line chart
• 3. 3D map and contour map

# 1. Pyplot basic syntax

(1) Create canvas
Figure () creates a blank canvas. You can specify the size of the canvas, figsize, and set the resolution DPI

plt.figure()
PLT. Figure (figsize = (8,6), DPI = 80) ා (8,6) represents the length and width of the image in inches, and DPI represents the resolution

(2) Create subgraph
Figure. Add ﹣ subplot method is used to create a subplot. You can also use subplot directly

fig=plt.figure()
or
subplot(2,2,1)
For example:
plt.figure()
x=np.linspace(-np.pi,np.pi,100)
y1=np.sin(x)
y2=np.cos(x)
PLT. Subplot (211) represents the first one in the 2x1 canvas
plt.plot(x,y1,'-r')
plt.title('sin')
Plot. Subplot (212) is equal to the second one in Fig. add sub plot (212) 2x1 canvas
plt.title('cos')
plt.plot(x,y2,'-.b')
plt.show()

(3) Add canvas content
Be careful:
For Chinese code scrambling, you can add the following code before drawing:

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

Adding legend must be after plot, setting label and so on has no sequence with plot, and the sequence in legend is consistent with that of plot.

function Effect
plt.xlabel Set the name of the x-axis
plt.ylabel Set the name of the y-axis
plt.xlim Set the range of X axis (the parameter is tuple of upper and lower limit)
plt.ylim Set Y-axis range
plt.xticks Set the number and value of x-axis scale (parameter is list) [its parameter rotation represents the tilt angle]
plt.yticks Set the scale number and value of y-axis
plt.legend Set legend
plt.grid Set gridlines

Example (apply all of the above settings):

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
fig=plt.figure()
plt.title('sin&cos')
x=np.linspace(-np.pi,np.pi,100,endpoint=False)
y1=np.sin(x)
y2=np.cos(x)
PLT. Plot (x, Y1, '- R') (solid line)
PLT. Plot (x, Y2, '-. B') (dotted line)
a=[-np.pi,-1/2*np.pi,0,1/2*np.pi,np.pi]
Label = [R '$- \ PI$', R '$- \ pi / 2$', 0, R '$\ pi / 2$', R '$\ PI$'] (using latex)
Plt.x ticks (a, label) 񖓿 set the mark of x-axis. If there is only a, set the mark of x-axis. There is another parameter, rotation, which represents the tilt angle. If the scale label of x-axis is too long, set the angle to tilt it, for example, rotation = 45, which represents the tilt 45 degree
PLT. Xlim ((0, NP. PI)) (set the display range of X axis [0, PI)
PLT. Xlabel ('value of X ')
PLT. Ylabel ('value of Y ')
PLT. Grid (linestyle = ':', color ='y ') to add background gridlines
PLT. Legend (['sin ','cos'], LOC ='lower left'). Adjust the position of the legend to the bottom left. Pay attention to the sin in the legend and the order of drawing corresponding to Cos
plt.show()

Result:

(4) Saving and displaying drawings
Savefig can be used to save the drawing
Show() is used to display graphics

Plt.savefig ('1.png') can add path

# 2. Scatter chart and line chart

Scatter chart can be drawn by scatter function, which is specially used to draw scatter chart, or plot function.
Use plot to directly remove the part about connecting line, and add marker part to change from line chart to scatter chart. For the example above:

plt.plot(x,y1,'-r')
Change it to PLT. Plot (x, Y1, 'ro') and it becomes a scatter diagram (O for points)
You can also use the scatter function
plt.scatter(x,y1,c='r',marker='o')

Result:

# 3. 3D map and contour map

For 3D drawing, it is necessary to introducefrom mpl_toolkits.mplot3d import Axes3D
The difference between 3D graph and 2D graph is that X and y are meshgrid operated
The mesh grid function is used to weave x, y vectors into a two-dimensional grid
For example:
In two-dimensional coordinate system, if the value of X axis is 1, 2, 3, and the value of Y axis is 4, 5, then (1, 4) (1, 5) can be obtained
（2，4）（2，5）
(3, 4) (3, 5) these coordinates constitute a two-dimensional grid, and the role of meshgrid is this.

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
a=np.array([1,2,3])
b=np.array([4,5])
c,d=np.meshgrid(a,b)  #c[[1,2,3],[1,2,3]] d[[4,4,4],[5,5,5]]
e=np.array([[1,2,1],[7,8,9]])
fig=plt.figure()
#Add a 3D axis to the canvas
ax=Axes3D(fig)
#Draw a 3D diagram: rstride and cstride represent the span of row and column respectively. Generally, the default line is selected and not set.
ax.plot_surface(c,d,e,rstride=1,cstride=1,cmap='rainbow')
Ax. Scatter (C, D, e, C ='g ', marker =' ^ ')

Result:

I feel that for 3D drawing, because x-axis and y-axis are two-dimensional arrays, when drawing according to two-dimensional array in 2D, it will be horizontally divided and then connected. In 3D, the first step is to segment horizontally, then connect according to the order of composition points, then divide according to the longitudinal direction, and then connect according to the order of composition points, so as to connect into 3D graph. All we need to do for drawing 3D graph is to form grid matrix, and then calculate Z value according to x, y value, so that we can draw 3D graph after X, y, Z have all of them.
Be careful:
When x and y are two-dimensional arrays, they will be horizontally divided and then connected.

x=np.array([[0,0.5,1],[0,0.5,1],[2,1,4]])
y=np.array([[0,0,0],[1,1,1],[3,3,4]])
plt.plot(x,y,'-rx')
Connect according to (0,0) (0,1) (2,3), then (0.5,0) (0.5,1) (1,3), and so on

2D results:

Three dimensional is equivalent to more Z, let z = NP. Array ([[0,1,2], [1,2,3], [4,5,6]])
Then the drawing can be understood as:
Connect according to (0,0,1) (0.5,1,1) (2,3,4), then (0.5,0,1) (0.5,1,2) (1,3,5) and so on, then connect horizontally (0,0,1) (0.5,0,1) (1,0,2) and so on.
It can be understood as connecting after horizontal division and then connecting after vertical division.

Contour map:

plt.figure()
plt.xlabel(r'$\theta_0$')
plt.ylabel(r'$\theta_1$')
theta0_val=np.linspace(-10,10,100)
theta1_val=np.linspace(-1,4,100)
xs,ys=np.meshgrid(theta0_val,theta1_val)
#Use contour to generate contour map of three-dimensional structure surface, np.logspace (- 2,3,20) represents the 20 element sequence of Z value 10 ^ - 2 to 10 ^ 3, that is, those contours
CS = plt.contour(xs, ys, J_val, np.logspace(-2,3,20))
#Finally, add label, inline to control whether the label is drawn online, and the font size is 10.
plt.clabel(CS, inline=1, fontsize=10)
#Draw the best solution
plt.plot(theta[0,0], theta[1,0], 'rx', markersize=10, linewidth=2)
#Theta [0,0], theta [1,0] is the value of the optimal solution
plt.show()

Result:

## Troubleshooting for 1 hour delay of task scheduling on easyscheduler

1、 Background In the morning, the irascible King w came with a message: “the hour calculation task was delayed for one hour, resulting in the task of calculating 3 data that should be started at 6:00 to be submitted for execution at 7:00, while the task of calculating 4 data ran twice to help troubleshoot […]