> For the complete documentation index, see [llms.txt](https://porter.gitbook.io/deep-learning-series/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://porter.gitbook.io/deep-learning-series/index-2/3.3-matplotlib-han-shu.md).

# 3.3 matplotlib函数

本节主要是学习matplotlib的函数调用，主要是基础的学习路线，包括简单的实例笔记等。

* [ ] [摘要](https://porter.gitbook.io/deep-learning-series/index-2/pages/-Lkm6Rt9-g5LIFrsnGpc#摘要)
  * [interactive模式特点：](https://porter.gitbook.io/deep-learning-series/index-2/pages/-Lkm6Rt9-g5LIFrsnGpc#interactive模式特点)
    * [plt.ion()](/deep-learning-series/index-2/3.3-matplotlib-han-shu.md#pltion)
    * [plt.ioff()](/deep-learning-series/index-2/3.3-matplotlib-han-shu.md#pltioff)
  * [常用函数](https://porter.gitbook.io/deep-learning-series/index-2/pages/-Lkm6Rt9-g5LIFrsnGpc#常用函数)
    * [plt.figure()](/deep-learning-series/index-2/3.3-matplotlib-han-shu.md#pltfigure)
      * [Parameters:](/deep-learning-series/index-2/3.3-matplotlib-han-shu.md#parameters)
      * [Returns:](/deep-learning-series/index-2/3.3-matplotlib-han-shu.md#returns)
    * [plt.plot()](/deep-learning-series/index-2/3.3-matplotlib-han-shu.md#pltplot)
* [x] Edit By Porter, 积水成渊,蛟龙生焉。

## interactive模式特点：

### plt.ion()

### plt.ioff()

开启interactive模式，用plt.ion()，放在绘图之前，关闭该模式用plt.ioff()；

python可视化库matplotlib有两种显示模式：

* 阻塞（block）模式
* 交互（interactive）模式

在Python Consol命令行中，默认是交互模式。而在python脚本中，matplotlib默认是阻塞模式。

在交互模式下, plt.plot(x)或plt.imshow(x)是直接出图像，不需要plt.show() 如果在脚本中使用ion()命令开启了交互模式，没有使用ioff()关闭的话，则图像会一闪而过，并不会常留。要想防止这种情况，需要在plt.show()之前加上ioff()命令。

在阻塞模式下, 打开一个窗口以后必须关掉才能打开下一个新的窗口。这种情况下，默认是不能像Matlab一样同时开很多窗口进行对比的。 plt.plot(x)或plt.imshow(x)是直接出图像，需要plt.show()后才能显示图像

下面这个例子讲的是如何像matlab一样同时打开多个窗口显示图片或线条进行比较，同时也是在脚本中开启交互模式后图像一闪而过的解决办法：

```python
    import matplotlib.pyplot as plt
    plt.ion()    # 打开交互模式
    # 同时打开两个窗口显示图片
    plt.figure()
    plt.imshow(i1)
    plt.figure()
    plt.imshow(i2)
    # 显示前关掉交互模式
    plt.ioff()
    plt.show()
```

## 常用函数

### plt.figure()

#### Parameters:

* num : integer or string, optional, default: None

If not provided, a new figure will be created, and the figure number will be incremented. The figure objects holds this number in a number attribute. If num is provided, and a figure with this id already exists, make it active, and returns a reference to it. If this figure does not exists, create it and returns it. If num is a string, the window title will be set to this figure's num.

* figsize : (float, float), optional, default: None

width, height in inches. If not provided, defaults to rcParams\["figure.figsize"] = \[6.4, 4.8].

* dpi : integer, optional, default: None

resolution of the figure. If not provided, defaults to rcParams\["figure.dpi"] = 100.

* facecolor :

the background color. If not provided, defaults to rcParams\["figure.facecolor"] = 'w'.

* edgecolor :

the border color. If not provided, defaults to rcParams\["figure.edgecolor"] = 'w'.

* frameon : bool, optional, default: True

If False, suppress drawing the figure frame.

* FigureClass : subclass of Figure

Optionally use a custom Figure instance.

* clear : bool, optional, default: False

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

#### Returns:

* figure : Figure

The Figure instance returned will also be passed to new\_figure\_manager in the backends, which allows to hook custom Figure classes into the pyplot interface. Additional kwargs will be passed to the Figure init function.

### plt.plot()
