matplotlib 之 plt.figure

版权声明:本文为博主原创文章,欢迎讨论共同进步。 https://blog.csdn.net/tz_zs/article/details/81091243

____tz_zs

matplotlib.pyplot.figure

创建一个图形(figure)对象。

官网 matplotlib.pyplot.figure

def figure(num=None,  # autoincrement if None, else integer from 1-N
           figsize=None,  # defaults to rc figure.figsize
           dpi=None,  # defaults to rc figure.dpi
           facecolor=None,  # defaults to rc figure.facecolor
           edgecolor=None,  # defaults to rc figure.edgecolor
           frameon=True,
           FigureClass=Figure,
           clear=False,
           **kwargs
           ):

参数:

num : 图像的编号(名称),参数没有提供时,会自动的维持一个自增的数字名称。当提供了参数时,如果此figure存在,则返回此对象,如果不存在,则创建并返回figure。

figsize : 整数型的元组格式,默认为 None,以元组形式(宽,高)提供宽高的大小,单位为英尺,若没有提供,则大小为 rc figure.figsize 定义的值。

dpi : 整数,默认为 None (默认值 rc figure.dpi.),设置 figure 的分辨率。

facecolor : 背景颜色,默认为  rc figure.facecolor. 的值。

edgecolor : 边框颜色,默认为 rc figure.edgecolor. 的值。

frameon : 是否显示绘制图框。

FigureClass : 从matplotlib.figure.Figure派生的类,可用于自定义Figure实例。

clear:bool,可选,默认为False。如果为True并且已经存在figure,那么它将被清除。

返回值:

figure : Figure
返回的这个 Figure 实例也将被传递给后端的 new_figure_manager,这将允许将自定义的 Figure 类挂钩到 pylab 接口中。额外的 kwargs 参数将传递给 figure 的 init 函数

提醒:

如果你创建了很多图,建议显示的调用“close”,以便使pylab能够正确的清理内存。
rcParams定义了默认值,可以在matplotlibrc文件中修改这些值。

使用例子:

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1, 100, 100)
y1 = np.random.randint(20, 60, size=100)
y2 = np.random.randint(30, 70, size=100)
y3 = np.random.randint(50, 90, size=100)

plt.figure(num="111", figsize=(6, 4), facecolor="pink", edgecolor="green")
plt.plot(x, y1, c="red", label="y1_low")
plt.plot(x, y2, c="blue", label="y2_middle")
plt.plot(x, y3, c="yellow", label="y3_high")
plt.legend(loc="best")
plt.show()

.

.

new_figure_manager 代码逻辑

pyplot.py

## Figures ##

def figure(num=None,  # autoincrement if None, else integer from 1-N
           figsize=None,  # defaults to rc figure.figsize
           dpi=None,  # defaults to rc figure.dpi
           facecolor=None,  # defaults to rc figure.facecolor
           edgecolor=None,  # defaults to rc figure.edgecolor
           frameon=True,
           FigureClass=Figure,
           clear=False,
           **kwargs
           ):
    ......
    ......
    ......
    figManager = _pylab_helpers.Gcf.get_fig_manager(num)
    if figManager is None:
        ......
        ......   
        ......

        figManager = new_figure_manager(num, figsize=figsize,
                                        dpi=dpi,
                                        facecolor=facecolor,
                                        edgecolor=edgecolor,
                                        frameon=frameon,
                                        FigureClass=FigureClass,
                                        **kwargs)

        ......
        ......
        ......
    if clear:
        figManager.canvas.figure.clear()

    return figManager.canvas.figure

.

clear 部分的代码逻辑

pyplot.py

## Figures ##

def figure(num=None,  # autoincrement if None, else integer from 1-N
           figsize=None,  # defaults to rc figure.figsize
           dpi=None,  # defaults to rc figure.dpi
           facecolor=None,  # defaults to rc figure.facecolor
           edgecolor=None,  # defaults to rc figure.edgecolor
           frameon=True,
           FigureClass=Figure,
           clear=False,
           **kwargs
           ):
    ......
    ......
    ......
    ......
    ......
    ......
    if clear:
        figManager.canvas.figure.clear()

    return figManager.canvas.figure

.

figure.py

class Figure(Artist):

    """
    The Figure instance supports callbacks through a *callbacks*
    attribute which is a :class:`matplotlib.cbook.CallbackRegistry`
    instance.  The events you can connect to are 'dpi_changed', and
    the callback will be called with ``func(fig)`` where fig is the
    :class:`Figure` instance.

    *patch*
       The figure patch is drawn by a
       :class:`matplotlib.patches.Rectangle` instance

    *suppressComposite*
       For multiple figure images, the figure will make composite
       images depending on the renderer option_image_nocomposite
       function.  If suppressComposite is True|False, this will
       override the renderer.
    """
    ......
    ......
    ......
    ......
    ......
    def clf(self, keep_observers=False):
        """
        Clear the figure.

        Set *keep_observers* to True if, for example,
        a gui widget is tracking the axes in the figure.
        """
        self.suppressComposite = None
        self.callbacks = cbook.CallbackRegistry()

        for ax in tuple(self.axes):  # Iterate over the copy.
            ax.cla()
            self.delaxes(ax)         # removes ax from self._axstack

        toolbar = getattr(self.canvas, 'toolbar', None)
        if toolbar is not None:
            toolbar.update()
        self._axstack.clear()
        self.artists = []
        self.lines = []
        self.patches = []
        self.texts = []
        self.images = []
        self.legends = []
        if not keep_observers:
            self._axobservers = []
        self._suptitle = None
        self.stale = True

    def clear(self, keep_observers=False):
        """
        Clear the figure -- synonym for :meth:`clf`.
        """
        self.clf(keep_observers=keep_observers)
    ......
    ......
    ......
    ......
    ......

.

end

猜你喜欢

转载自blog.csdn.net/tz_zs/article/details/81091243