figure函数--Matplotlib

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是一个字符串,画布的标签与窗口的标题使用该字符串命名。

  1. 参数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参数

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Python中,matplotlib是一个常用的绘图库。它提供了各种函数和方法来创建和定制图形。其中一些函数和方法包括: 1. 函数figure的方法:figure()函数用于创建一个新的图形对象,而figure对象的方法可以用来设置图形的标题、x轴标签和y轴标签。例如,suptitle()方法用于设置图形的标题,supxlabel()方法用于设置x轴标签,supylabel()方法用于设置y轴标签。\[1\] 2. 函数subplots:subplots()函数用于创建一个包含多个子图的图形。它可以指定子图的行数、列数以及其他参数。这个函数返回一个包含所有子图的figure对象和一个包含所有子图的axes对象的数组。可以使用这些axes对象来绘制和定制每个子图。\[2\] 另外,如果你想了解更多关于matplotlib的函数和方法,你可以查看plot()函数的文档,其中包含了关于线的类型的说明。你还可以使用axis()命令来方便地获取和设置XY轴的一些属性。\[3\] #### 引用[.reference_title] - *1* *2* [Python--matplotlib(持续更新)](https://blog.csdn.net/abc31431415926/article/details/127910035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python常用数据作图--matplotlib用法(相关设置及常用图)](https://blog.csdn.net/qiuqiu1027/article/details/106545178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值