figure函数–Matplotlib
函数功能:
Create a new figure, or activate an existing figure.
创建一个新图形或者激活一个现有图形
函数语法:
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class ‘matplotlib.figure.Figure’>, clear=False, **kwargs)
函数参数:
num: int or str, optional
A unique identifier for the figure.
If a figure with that identifier already exists, this figure is made active and returned. An integer refers to the Figure.number attribute, a string refers to the figure label.
图形标识符: 可选参数,整数或者字符串,画布的唯一标识。
如果具有该标识符的画布已经存在,则将该画布激活并返回。整数表示 Figure.number属性,字符串表示图形标签。
If there is no figure with the identifier or num is not given, a new figure is created, made active and returned. If num is an int, it will be used for the Figure.number attribute, otherwise, an auto-generated integer value is used (starting at 1 and incremented for each new figure). If num is a string, the figure label and the window title is set to this value.
若该标识符无对应图形或者参数num没有指定,会创建一个新的画布,并激活/返回。若参数num为整数,Figure.number中的number以指定的该数字命名;若无指定标识符,使用自动生成的整数值(从1开始,递增)。若参数num是一个字符串,画布的标签与窗口的标题使用该字符串命名。
- 参数num为数字
import matplotlib.pyplot as plt
fig = plt.figure(num=4, figsize=(2, 2), edgecolor='r')
plt.show()
2. 参数num为字符串
import matplotlib.pyplot as plt
fig = plt.figure(num='test', figsize=(2, 2), edgecolor='r')
plt.show()
figsize: (float, float), default: rcParams[“figure.figsize”] (default: [6.4, 4.8])
Width, height in inches.
画布大小:一对浮点型坐标,宽度,高度以英寸为单位。默认为[6.4, 4.8].
import matplotlib.pyplot as plt
fig1 = plt.figure(num='test1', figsize=(2,1))
fig2 = plt.figure(num='test2', figsize=(4,2))
plt.show()
dpi: float, default: rcParams[“figure.dpi”] (default: 100.0)
The resolution of the figure in dots-per-inch.
分辨率: 画布的分辨率,以每英寸点数为单位。默认为:100
相同的figsize,dpi越大,画布越大
import matplotlib.pyplot as plt
fig1 = plt.figure(num='test1', figsize=(4,2), dpi=100)
fig2 = plt.figure(num='test2', figsize=(4,2), dpi=200)
plt.show()
facecolor: color, default: rcParams[“figure.facecolor”] (default: ‘white’)
The background color.
画布背景颜色: 默认为: 白色
import matplotlib.pyplot as plt
fig = plt.figure(num='test', figsize=(5,4), dpi=100,facecolor='y',edgecolor='r')
plt.show()
edgecolor: color, default: rcParams[“figure.edgecolor”] (default: ‘white’)
The border color.
import matplotlib.pyplot as plt
# fig1 = plt.figure(num='test1', figsize=(10,10), dpi=50, facecolor='r', frameon=True)
fig2 = plt.figure(num='test2', figsize=(4,2), dpi=200, facecolor='c', edgecolor='r',frameon=True,linewidth=5)
plt.show()
frameon: bool, default: True
If False, suppress drawing the figure frame.
画布框架:布尔型,默认:True,若为False,禁止绘制图形框架
从图中可以看出画布框架即上图红色边框里面的部分,当frameon-=False,则不能显示背景颜色与边界颜色。
import matplotlib.pyplot as plt
# fig1 = plt.figure(num='test1', figsize=(10,10), dpi=50, facecolor='r', frameon=True)
fig2 = plt.figure(num='test2', figsize=(4,2), dpi=200, facecolor='c', edgecolor='r',frameon=False,linewidth=5)
plt.show()
FigureClass: subclass of Figure
Optionally use a custom Figure instance.(不清楚)
clear: bool, default: False
If True and the figure already exists, then it is cleared.
清除:布尔型,默认:False.若为True,将清除已存在画布。
以下参数待研究:
tight_layout: bool or dict, default: rcParams[“figure.autolayout”] (default: False)
If False use subplotpars. If True adjust subplot parameters using tight_layout with default padding. When providing a dict containing the keys pad, w_pad, h_pad, and rect, the default tight_layout paddings will be overridden.
constrained_layout: bool, default: rcParams[“figure.constrained_layout.use”] (default: False)
If True use constrained layout to adjust positioning of plot elements. Like tight_layout, but designed to be more flexible. See Constrained Layout Guide for examples. (Note: does not work with add_subplot or subplot2grid.)
kwargs : optional
See Figure for other possible arguments.
官方文档: figure参数