When learning Python various toolkits, see the online tutorials always feel a lot of various ways is very complex, but also very many types and number of parameters to understand it takes a lot of time.
So here I am, methods and each parameter is explained in detail by several examples, so for 0-based people can quickly grasp Matplotlib basic graphics operations.
- Numpy package is first introduced and matplotlib
Import numpy AS NP Import matplotlib.pyplot AS PLT % matplotlib inline # so that graphics can be displayed directly on the notebook
- First, draw a basic graphics
plt.plot ([1,2,3,4,5], [1,4,9,16,25]) # Plot graphics rendering method, the first parameter is the value of the x-axis, the second parameter is y-axis value corresponding plt.xlabel ( ' the this iS-axis x ' , fontsize = 16) # the xlabel designated the x-axis is the name of the specified font size fontsize plt.ylabel ( ' the this iS-axis y ', fontsize = 16)
- Character parameters of different types of lines; character parameters of different colors
- To plot method specified incoming lines and color parameters
plt.plot ([1,2,3,4,5], [1,4,9,16,25], ' GV ' ) # where g equals 'gv' in green green, v representative of the triangular plt. the xlabel ( ' the xlabel ' , fontSize = 16 ) plt.ylabel('ylabel',fontsize = 16)
- In addition to such a method of the above parameters, the parameters can also be separated to give
plt.plot ([1,2,3,4,5], [1,4,9,16,25], ' + ' , Color = ' R & lt ' ) # where the '+ ' represents a plus point, r Representative red red plt.xlabel ( ' the xlabel ' , fontSize = 16 ) plt.ylabel('ylabel',fontsize = 16)
- Let's draw multiple lines
= np.arange tang_numpy (0,10,0.5) # production from 0 to 10 at intervals of 0.5 arithmetic sequence of # and then draw continuous three lines, called three times plot method plt.plot (tang_numpy, tang_numpy, ' r-- ' ) plt.plot (tang_numpy, tang_numpy * 2, ' bs ' ) plt.plot(tang_numpy,tang_numpy*3,'go')
- May be used only once plot method of drawing the three lines, the result is the same
plt.plot(tang_numpy,tang_numpy,'r--', tang_numpy,tang_numpy*2,'bs', tang_numpy,tang_numpy*3,'go')
- Then draw a cosine function, more of the given parameters, the comment will explain the role of each parameter
np.linspace = X (-10,10) # range of lines Y = np.sin (X) # as linewidth parameters are line width, color is the color, linestype a line type, marker type is labeled, makercolor is labeled color, markersize a size marker, alpha transparency plt.plot (X, Y, as linewidth =. 5, color = ' B ' , lineStyle = ' - ' , marker = ' O ' , markerfacecolor = ' R & lt ' , markersize. 8 = , alpha = 0.4)
- Subgraph, a pattern may be formed by a combination of a plurality of sub-graphs, it can be viewed as a whole a matrix of FIG, and wherein each element is a subgraph of the matrix
# 211 would be represented by a figure drawn by the last row 12 is a representation of a first sub-graph among the one in FIG plt.subplot (211 ) plt.plot(x,y,color='r') # 212 would be represented by a figure drawn by the last row 12 is represented by a subgraph among second FIG plt.subplot (212 ) plt.plot(x,y,color='b')
# 211 would be represented by a figure drawn by the last row 11 is represented by two sub-graph among the 1st FIG plt.subplot (121 ) plt.plot(x,y,color='r') # 212 would be represented by a figure drawn by the last row 11 is represented by two sub-graph among the second FIG plt.subplot (122 ) plt.plot(x,y,color='b')
# 321 would be represented by a figure drawn by the last row 13 is represented by two sub-graph among the 1st FIG plt.subplot (321 ) plt.plot (X, Y, Color = ' R & lt ' ) # 321 would be represented by a figure drawn by the last two rows 43 are represented among the subgraph of FIG. 4 plt.subplot (324 ) plt.plot(x,y,color='b')
- Figure to add some text comments
plt.plot(x,y,color='b',linestyle=':',marker = 'o',markerfacecolor='r',markersize = 10) plt.xlabel('x:---') plt.ylabel ( ' : --- y ' ) plt.title('this is the title:---') plt.text (0,0, ' the this text The IS ' ) # (0,0) is a text position plt.grid (True) # This is a grid # XY position is pointing arrow, xytext position is annotated arrowprops attribute specifies the arrow is plt.annotate ( ' tangyudi ' , XY = (- 5,0), xytext = (- 2, 0.3), arrowprops = dict (facecolor = ' Red ' , Shrink = 0.05, headlength = 20 is, headwidth = 20))
- Picture style settings
x = np.linspace(-10,10) and = np.sin (x) plt.style.use ( ' dark_background ' ) # to specify an image style plt.plot (x, y)
- A histogram
np.random.seed (0) # Set random seed X np.arange = (. 5 ) y = np.random.randint (5,5,5 ) Fig, axes = plt.subplots (ncols = 2) # has two subgraphs of v_bars axes = [0] .bar (X, Y, Color = ' Blue ' ) # axes [0] represents a first sub FIG h_bars axes = [. 1] .barh (X, Y, Color = ' Red ' ) axes [0] .axhline (0, Color = ' Gray ' , as linewidth = 2) # plus one horizontal axes [. 1] .axvline (0, Color = ' Gray ' , as linewidth = 2) # plus a vertical line
fig,ax = plt.subplots() v_bars = ax.bar (X, Y, Color = ' LightBlue ' ) # of columnar color value is less than 0 Green for bar, height in ZIP (v_bars, Y): # bar, the height of the columnar height value IF height < 0: bar.set(edgecolor = 'darkred',color = 'green',linewidth = 3) ax.axhline (0, Color = ' Red ' ) # where one horizontal line plus 0