首页 > 解决方案 > Matplotlib plt.xlabel() 与 ax.set_xlabel()

问题描述

我正在使用一些在 Python 中使用 matplotlib 的单例版本的代码,即它有类似的调用

plt.figure()
...
plt.xlabel("abc")

我正在尝试将其转换为功能/无内存版本:

fig,ax = plt.subplots()
...
ax.set_xlabel("abc")

几个问题:

标签: pythonoopmatplotlib

解决方案


Matplotlib 有 pyplot 接口,后来开发了面向对象的接口,如文档(https://matplotlib.org/3.2.1/tutorials/introductory/lifecycle.html)中所述。

现在首选后者,因此使用 fig, ax = plt.subplots 然后在 ax 上使用 setter 方法。


推荐阅读