matplotlib之figure()详解

您所在的位置:网站首页 figure百度翻译 matplotlib之figure()详解

matplotlib之figure()详解

#matplotlib之figure()详解| 来源: 网络整理| 查看: 265

最最基础的一个方法了plt.figure(),基本绘图的时候第一句话就是这个,借一句官网的话:Create a new figure, or activate an existing figure。该函数的功能就是为了创建一个图形figure,或者激活一个已经存在的图形figure,其完整的参数调用如下,下面一个个介绍其关键字。

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=, clear=False, **kwargs) 1. num:int or str, optional

合法值为整数或者一个字符串,可选。官网已经说的非常清楚了,这里只是翻译一下,该关键字是一个figure独一无二的ID标识,当num是整数的时候,该整数即为Figure.number属性的值,如果是字符串,则Figure.number属性的值自动加1。下面看一个例子:

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.

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.

 该例子展示了plt.figure()中关键字num的作用,当存在多个figure的时候,就可以通过调用plt.figure(),传入对应的ID标识即可激活对应的figure,其中figure是有对应的编号的,可以传入数字来激活。

x = np.linspace(-np.pi, np.pi, 256) y1 = np.sin(x) y2 = np.cos(x) fig1 = plt.figure(num='first') fig1.suptitle('first figure') plt.plot(x, y1) fig2 = plt.figure(num='second') fig2.suptitle('second figure') plt.plot(x, y2) plt.figure(num=1) #plt.figure(num='first') plt.plot(x, y2) plt.show()

2. figsize:(float, float)

合法值为一个元组(float, float), default: rcParams["figure.figsize"] (default: [6.4, 4.8]),分别表示宽和高,整型会被转化为浮点型float。

fig1 = plt.figure(figsize=(2,1)) fig1.suptitle('fig1') plt.plot([1,2,3,4]) fig2 = plt.figure(figsize=(4,2)) fig2.suptitle('fig2') plt.plot([1,2,3,4]) plt.show()

3. facecolor

背景色(background color),default: rcParams["figure.facecolor"] (default: 'white'),默认白色。

fig1 = plt.figure(facecolor='b') 4.clear

合法值为bool类型,default: False,主要是用来清除已经存在的figure。

If True and the figure already exists, then it is cleared.

5. 返回值

返回一个图形figure句柄。

 

将图形保存为文件

matplotlib的一个优点就是能够将图形保存为各种不同的数据格式,可以通过savefig()命令将绘制的图形保存成文件存在磁盘上。例如:

fig.savfig('my_figure.png')

matplotlib支持许多图形格式,具体格式由操作系统已安装的图形显示接口决定,可以通过canvas对象的方法查看系统支持的文件格式。

需要注意的是,当你保存图形文件时,不需要使用plt.show()了。

 

 

 

 

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3