matplotlib command and format: image (figure) and sub-region (axes) layout and planning

Source: Internet
Author: User
Tags border color
1.figure Syntax and Operation

(1) Figure Grammar Description

Figure (Num=none, Figsize=none, Dpi=none, Facecolor=none, Edgecolor=none, Frameon=true)
Num: Image number or name, number is number, string is name Figsize: Specifies the width and height of the figure, in inches; the DPI parameter specifies the resolution of the drawing object, that is, how many pixels per inch, the default value is Facecolor: Background color edgecolor: Border color Frameon: Show borders
(2) Examples:
Import Matplotlib.pyplot as Plt
Create a custom image
Fig=plt.figure (figsize= (4,3), facecolor= ' Blue ')
Plt.show ()2.subplot Create a single child graph (1) Subplot grammarSubplot (NROWS,NCOLS,SHAREX,SHAREY,SUBPLOT_KW,**FIG_KW)


subplot can plan figure to be divided into N children, but each subplot command will only create a child graph, refer to the example below.
(2) Examples
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 Create multiple child graphs (1) Subplots grammarThe subplots parameter is similar to subplots
(2) ExamplesImport 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 child maps or regionsAdd_subplot and add_axes are both face object figure programming, Pyplot API does not have this command
(1) The Add_subplot parameters of Add_subplot new sub graph are similar to those of 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 subregion, which can be located anywhere within the figure, and the area can be arbitrarily set size add_axes parameters can refer to the official documentation: 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的百分比, starting from Figure 10%, the width is figure 80%.
Left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# get the 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 the drawn handle
AX2 = Fig.add_axes ([Left, bottom, width, height])
Ax2.plot (x,y, ' B ')
Ax2.set_title (' area2 ')
Plt.show ()






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.