1.figure Syntax and Operation
(1) Figure Syntax Description
Figure (Num=none, Figsize=none, Dpi=none, Facecolor=none, Edgecolor=none, Frameon=true)
Num: Image number or name, number number, string name Figsize: Specifies the width and height of the figure, in inches, and the DPI parameter specifies the resolution of the drawing object, that is, the number of pixels per inch, the default value is Facecolor: Background color edgecolor: Border color Frameon: Whether the border is displayed
(2) Example:
Import Matplotlib.pyplot as Plt
Create a custom image
Fig=plt.figure (figsize= (4,3), facecolor= ' Blue ')
Plt.show ()2.subplot Creating a single sub-graph(1) Subplot grammar subplot (NROWS,NCOLS,SHAREX,SHAREY,SUBPLOT_KW,**FIG_KW)
Subplot can plan a figure divided into N sub-graphs, but each subplot command will only create a sub-graph, refer to the example below.
(2) Example
Import NumPy as NP
Import Matplotlib.pyplot as Plt
x = Np.arange (0, 100)
#作图1
Plt.subplot (221)
Plt.plot (x, X)
#作图2
Plt.subplot (222)
Plt.plot (x,-X)
#作图3
Plt.subplot (223)
Plt.plot (x, x * * 2)
Plt.grid (color= ' R ', linestyle= '--', linewidth=1,alpha=0.3)
#作图4
Plt.subplot (224)
Plt.plot (x, Np.log (x))
Plt.show ()
3.subplots Creating multiple sub-graphs(1) Subplots syntax subplots parameters are similar to subplots
(2) Example import NumPy as NP
Import Matplotlib.pyplot as Plt x = Np.arange (0, 100)
#划分子图
Fig,axes=plt.subplots (2,2)
ax1=axes[0,0]
ax2=axes[0,1]
ax3=axes[1,0]
ax4=axes[1,1]
#作图1
Ax1.plot (x, X)
#作图2
Ax2.plot (x,-X)
#作图3
Ax3.plot (x, x * * 2)
Ax3.grid (color= ' R ', linestyle= '--', linewidth=1,alpha=0.3)
#作图4
Ax4.plot (x, Np.log (x))
Plt.show ()
4. Object-oriented Api:add_subplots and add_axes new sub-map or regionAdd_subplot and Add_axes are both polygon object figure programming, Pyplot API does not have this command
(1) Add_subplot parameters of Add_subplot new sub-graph are similar to subplots
Import NumPy as NP
Import Matplotlib.pyplot as Plt
x = Np.arange (0, 100)
#新建figure对象
Fig=plt.figure ()
#新建子图1
Ax1=fig.add_subplot (2,2,1)
Ax1.plot (x, X)
#新建子图3
Ax3=fig.add_subplot (2,2,3)
Ax3.plot (x, x * * 2)
Ax3.grid (color= ' R ', linestyle= '--', linewidth=1,alpha=0.3)
#新建子图4
Ax4=fig.add_subplot (2,2,4)
Ax4.plot (x, Np.log (x))
Plt.show ()
(2) Add_axes new sub-area
Add_axes is a new sub-area, which can be located anywhere within the figure, and the area can be arbitrarily set size add_axes parameter can refer to the Official document: http://matplotlib.org/api/_as_gen/ Matplotlib.figure.figure.html#matplotlib.figure.figure
Import NumPy as NP
Import Matplotlib.pyplot as Plt
#新建figure
Fig = Plt.figure ()
# define Data
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
#新建区域ax1
#figure的百分比, drawing from the position of figure 10%, the width height is the figure of 80%
Left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# Get a drawn handle
AX1 = Fig.add_axes ([Left, bottom, width, height])
Ax1.plot (x, Y, ' R ')
Ax1.set_title (' area1 ')
#新增区域ax2, nested inside the AX1
Left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
# Get a drawn handle
AX2 = Fig.add_axes ([Left, bottom, width, height])
Ax2.plot (x, y, ' B ')
Ax2.set_title (' area2 ')
Plt.show ()