摘要
本节主要是学习matplotlib的函数调用,主要是基础的学习路线,包括简单的实例笔记等。
- [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一样同时打开多个窗口显示图片或线条进行比较,同时也是在脚本中开启交互模式后图像一闪而过的解决办法:
1 | import matplotlib.pyplot as plt |
常用函数
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.