plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
Parameter Description:
1.num: image code or name, number is code, string is name
2.figsize: width and height in feet
3.dpi: Specifies the resolution of the drawing object, that is, how many pixels per inch, the default value is 80
4.facecolor: background color
5.edgecolor: border color
6.frameon: Whether to display the frame
plt.subplots(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)
subplot can plan the figure to be divided into n subgraphs, but each subplot command will only create one subgraph
Parameter Description:
1.nrows: number of rows
2.ncols: number of columns
3.sharex: share the x-axis with whom
4.sharey: with whom to share the y-axis
5.subplot_kw: keyword dictionary
6. **fig_kw: other keywords
plt.subplot(ijn) form, where ij is the number of rows and columns, and n is the number of plots
For example:
# ax1 = fig.add_subplot(221), 221 # The first two represent the number of rows and columns divided by the canvas, which are divided into 4 subplots, and the last 1 is the representative. Now select the first subplot.
fig,axes=plt.subplots(n,n)
Divide the parent graph into nxn subgraphs
fig, axes = plt.subplots(23): It means that a 2*3 grid is created on the figure at one time, and plt.subplot() can only be added one by one
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))Indicates a pattern divided into 2x2, the first line is ax1, ax2; the second line is ax3, ax4
This is the basic method of the axes class, which plots the values of one array against the values of another array as lines or markers, the plot() method has optional formatted string parameters to specify the line type, marker color, style, and size.
The color codes are as follows:
'b' | blue |
'g' | green |
'r' | red |
'c' | blue |
'm' | Magenta |
'y' | yellow |
'k' | black |
'w' | White |
The marking symbols are as follows:
mark symbol | describe |
'.' | point marker |
'o' | circle mark |
'x' | 'X' mark |
'D' | diamond mark |
'H' | hex mark |
's' | square mark |
'+' | plus sign |
The line type represents characters, as shown in the following table:
character | describe |
'-' | solid line |
'--' | dotted line |
'-.' | Dotted line |
':' | dotted line |
'H' | hex mark |
ax.legend(handles, labels, loc)
Control legend:
- labels is a sequence of strings, used to specify the name of the label;
- loc is a parameter specifying the location of the legend, and its parameter value can be represented by a string or an integer;
- The handles parameter, which is also a sequence, contains all line types;
例如:ax.legend(labels = ('tv', 'Smartphone'), loc = 'lower right') # legend placed at lower right
The following is the expression method of the loc parameter, which is divided into two types: string and integer, as follows:
Location | string representation | integer number representation |
---|---|---|
adaptive | Best | 0 |
top right | upper right | 1 |
upper left | upper left | 2 |
lower left | lower left | 3 |
bottom right | lower right | 4 |
Right | right | 5 |
center left | center left | 6 |
center right | center right | 7 |
bottom center | lower center | 8 |
top center | upper center | 9 |
middle part | center | 10 |
Special layout case:
import matplotlib.gridspec as gridspec#调用网格
fig=plt.figure(num=1,figsize=(4,6))#创建画布
gs=gridspec.GridSpec(3,3)#设定网格
ax1=fig.add_subplot(gs[0,:])#选定网格
ax1.plot([1,2,3,4],[1,2,3,4])
ax2=fig.add_subplot(gs[1,:-1])
ax2.plot([1,2,3,4],[1,2,3,4])
ax3=fig.add_subplot(gs[1:,-1])
ax3.plot([1,2,3,4],[1,2,3,4])
ax4=fig.add_subplot(gs[2,0])
ax4.plot([1,2,3,4],[1,2,3,4])
ax5=fig.add_subplot(gs[2,1])
ax5.plot([1,2,3,4],[1,2,3,4])
plt.show()
ax.text(),参数讲解
- x,y: 放置text的位置,横纵坐标。
- s: str,text内容。
- fontsize: 设置字体大小,默认12,可选参数 [‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’,‘x-large’, ‘xx-large’]
- fontweight:设置字体粗细,可选参数 [‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’]
- alpha: 透明度,参数值0至1之间。
- rotation: (旋转角度)可选参数为:vertical,horizontal 也可以为数字。
- backgroundcolor:背景颜色。
- color: 字体颜色。
annotate():参数讲解
- s: 添加标注的内容,字符串形式。
- xy: 箭头指向的位置,就是我们想添加标注的对象,元组类型输入方式。
- xytext:添加标注的实际位置,标注实际所在位置,可看做箭头输出端。
- arrowprops: 此参数中提供箭头属性字典来绘制从文本到注释点的箭头。
width : 箭把宽度,整数或浮点数。
frac:箭头头部所占的比例,小于1。
headwidth:箭头头部宽度,整数或浮点数。
headlength: 箭头长度,整数或浮点数。
facecolor: 填充色 。
shrink:移动提示,并使其离注释点和文本一些距离,<1,大白话说就是,别让箭头两端
里标注点和文本太近。
- frontsize:可以设置字大小,这个参数遇到很多次了。
注:此函数需放置在show()函数之前。
#保存为jpg文件
plt.savefig("figure.jpg")#我这里填的是相对路径,如果想保存在指定文件夹下,填写绝对路径。
#保存为png文件
plt.savefig("figure.png")