matplotlib有两种绘图方式,一种是依托matplotlib.pyplot模块实现类似matlab绘图指令的绘图方式,一种是面向对象式绘图,依靠figurecanvas(画布)、 figure (图像)、 axes (轴域) 等对象绘图。
这两种方式之间并不是完全独立的,而是通过某种机制进行了联结,pylot绘图模式其实隐式创建了面向对象模式的相关对象,其中的关键是matplotlib._pylab_helpers模块中的单例类gcf,它的作用是追踪当前活动的画布及图像。
因此,可以说matplotlib绘图的基础是面向对象式绘图,pylot绘图模式只是一种简便绘图方式。
先不分析源码,先做实验!
实验
先通过实验,看一看我们常用的那些pyplot绘图模式
实验一
无绘图窗口显示
1
2
|
from matplotlib import pyplot as plt
plt.show()
|
实验二
出现绘图结果
1
2
3
|
from matplotlib import pyplot as plt
plt.plot([1,2])
plt.show()
|
实验三
出现绘图结果
1
2
3
|
from matplotlib import pyplot as plt
plt.gca()
plt.show()
|
实验四
出现绘图结果
1
2
3
4
|
from matplotlib import pyplot as plt
plt.figure()
# 或者plt.gcf()
plt.show()
|
pyplot模块绘图原理
通过查看pyplot模块figure()函数、gcf()函数、gca()函数、plot()函数和其他绘图函数的源码,可以简单理个思路!
- figure()函数:如果有现成图像,返回值就是当前图像,如果没有现成的图像,就初始化一个新图像,返回值为figure对象。
- gcf()函数:如果有现成图像,返回值就是当前图像,如果没有现成的图像,就调用figure()函数,返回值为figure对象。
- gca()函数:调用gcf()函数返回对象的gca方法,返回值为axes对象。
- plot()函数:调用gca()函数返回对象的plot方法。
- pyplot模块其他绘图函数:均调用gca()函数的相关方法。
因此,pyplot绘图模式,使用plot()函数或者其他绘图函数,如果没有现成图像对象,直接会先创建图像对象。
当然使用figure()函数、gcf()函数和gca()函数,如果没有现成图像对象,也会先创建图像对象。
更进一步,在matplotlib.pyplot模块源码中出现了如下代码,因此再查看matplotlib._pylab_helpers模块它的作用是追踪当前活动的画布及图像
1
2
|
figmanager = _pylab_helpers.gcf.get_fig_manager(num)
figmanager = _pylab_helpers.gcf.get_active()
|
matplotlib._pylab_helpers模块作用是管理pyplot绘图模式中的图像。该模块只有一个类——gcf,它的作用是追踪当前活动的画布及图像。
matplotlib.pyplot模块部分源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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:
max_open_warning = rcparams[ 'figure.max_open_warning' ]
if len(allnums) == max_open_warning >= 1:
cbook._warn_external(
"more than %d figures have been opened. figures "
"created through the pyplot interface "
"(`matplotlib.pyplot.figure`) are retained until "
"explicitly closed and may consume too much memory. "
"(to control this warning, see the rcparam "
"`figure.max_open_warning`)." %
max_open_warning, runtimewarning)
if get_backend().lower() == 'ps' :
dpi = 72
figmanager = new_figure_manager(num, figsize=figsize,
dpi=dpi,
facecolor=facecolor,
edgecolor=edgecolor,
frameon=frameon,
figureclass=figureclass,
**kwargs)
return figmanager.canvas.figure
def plot(*args, scalex= true , scaley= true , data=none, **kwargs):
return gca().plot(
*args, scalex=scalex, scaley=scaley,
**({ "data" : data} if data is not none else {}), **kwargs)
def gcf():
"" "
get the current figure.
if no current figure exists, a new one is created using
`~.pyplot.figure()`.
"" "
figmanager = _pylab_helpers.gcf.get_active()
if figmanager is not none:
return figmanager.canvas.figure
else :
return figure()
def gca(**kwargs):
return gcf().gca(**kwargs)
def get_current_fig_manager():
"" "
return the figure manager of the current figure.
the figure manager is a container for the actual backend-depended window
that displays the figure on screen.
if if no current figure exists, a new one is created an its figure
manager is returned.
returns
-------
`.figuremanagerbase` or backend-dependent subclass thereof
"" "
return gcf().canvas.manager
|
gcf类源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class gcf:
"" "
singleton to maintain the relation between figures and their managers, and
keep track of and "active" figure and manager.
the canvas of a figure created through pyplot is associated with a figure
manager, which handles the interaction between the figure and the backend.
pyplot keeps track of figure managers using an identifier, the "figure
number " or " manager number" (which can actually be any hashable value);
this number is available as the :attr:`number` attribute of the manager.
this class is never instantiated; it consists of an `ordereddict` mapping
figure/manager numbers to managers, and a set of class methods that
manipulate this `ordereddict`.
attributes
----------
figs : ordereddict
`ordereddict` mapping numbers to managers; the active manager is at the
end.
"" "
|
到此这篇关于详解matplotlib中pyplot和面向对象两种绘图模式之间的关系的文章就介绍到这了,更多相关matplotlib中pyplot和面向对象内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/mighty13/article/details/112691096