我更喜欢使用 Matplotlib 的面向对象的 API,直接对图形和坐标轴进行操作,而不是使用 matplotlib.pyplot
命令。
不过,我经常发现更容易找到有关如何通过 pyplot
界面做某事的文档。在这些情况下,找出相关的 pyplot
命令在 Figure 和 Axes 方法方面的作用是非常有用的,因为它会帮助我处理我去的时候遇到的任何特定的极端情况寻找文件。
例如,如果 ax
是 pyplot 中的“当前”轴,则 plt.xlabel()
等同于 ax.set_xlabel()
状态接口(interface)。但我在 xlabel()
文档中找不到任何提到 Axes.set_xlabel()
的内容。在这种情况下,在 Axes 文档中单独查找并不难,但映射会非常好。
是否有任何信息来源可以告诉我每个特定的 pyplot
命令在面向对象接口(interface)方面的作用?
最佳答案
使用来源,卢克!许多 pyplot 命令只是 Axes
或 Figure
方法的非常薄的包装器。我通常使用 IPython。
In [1]: import matplotlib.pyplot as plt
In [2]: plt.xlabel??
Signature: plt.xlabel(s, *args, **kwargs)
Source:
def xlabel(s, *args, **kwargs):
"""
Set the *x* axis label of the current axis.
Default override is::
override = {
'fontsize' : 'small',
'verticalalignment' : 'top',
'horizontalalignment' : 'center'
}
.. seealso::
:func:`~matplotlib.pyplot.text`
For information on how override and the optional args work
"""
return gca().set_xlabel(s, *args, **kwargs)
File: ~\appdata\local\programs\python\python36-32\lib\site-packages\matplotlib\pyplot.py
关于python - 是否有将 matplotlib 的有状态 pyplot API 映射到面向对象的等效项的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45209062/