问题描述
Matplotlib的 pyplot 文档说如下,
Matplotlib's pyplot documentation says as follow,
pyplot matplotlib.pyplot是matplotlib的基于状态的接口.
基于状态的接口到 matplotlib 的含义
What is meant by state-based interface to matplotlib
推荐答案
在 pyplot 教程中 它说
在matplotlib.pyplot中,函数调用会保留各种状态,以便跟踪当前图形和绘图区域之类的东西,并将绘图函数定向到当前轴
例如:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,6,5])
这会将pyplot置于定义了当前图形和当前轴的状态.随后发出一些其他 pyplot 命令,如
This puts pyplot in a state where a current figure and a current axes is defined. Subsequently issuing some other pyplot command like
plt.title("My title")
将设置存储在 pyplot 状态中的当前轴的标题.最后,
will set the title of the current axes that is stored in the pyplot state. Finally,
plt.show()
将显示存储在 pyplot 状态中的所有数字.(也相关: plt.show()如何知道要显示什么?)
will show all the figures stored in the pyplot state. (Also relevant: How does plt.show() know what to show?)
因此,总的来说,基于状态的接口意味着pyplot具有几个函数,这些函数将作用于当前定义的状态.这与使用对象方法的面向对象方法有着根本的不同:
So in total, the state-based interface means that pyplot has a couple of functions, which will act on a currently defined state. This is fundamentally different to an object-oriented approach, where object methods are used:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3],[4,6,5])
ax.set_title("My Title")
在这里,不同对象的方法用于创建新内容.(仍然可以通过pyplot创建该图,以便最终可以通过 plt.show()
进行显示.)
Here, different objects' methods are used to create new content. (Still the figure is created via pyplot, such that it can eventually be shown via plt.show()
.)
这篇关于matplotlib.pyplot 文档说它是 matplotlib 的基于状态的接口.什么是基于状态的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!