python - Matplotlib plt.xlabel() 与 ax.set_xlabel()

标签 python oop matplotlib

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

这样的调用
plt.figure()
...
plt.xlabel("abc")

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

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

几个问题:

  • 是否有直接设置轴的 xlabel 的选项?某物 像 ax.xlabel = "xlabel string" 吗?

  • From the documentation, it seems like this is not possible. (不是 甚至我们可以设置的私有(private)属性)

  • 还是总是需要通过 setter?这一直 把我弄糊涂了,让我觉得它不是 Pythonic 的。

  • 为什么 API 从 plt.xlabel() 更改为 ax.set_xlabel()

最佳答案

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

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

关于python - Matplotlib plt.xlabel() 与 ax.set_xlabel(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61035292/

相关文章:

python - Poetry 上的 ImportError 但已安装模块

python - unittest - 如何断言两个可能的 NaN 值是否相等

java - Java编译如何在另一个类中创建一个类的对象?

Python 在将鼠标悬停在一个点上时显示图像

python - 在 sympy 中使用 'exp' 时出错 -TypeError 并显示属性错误

python - 删除(从列表中)包含其他项目的所有项目 - Python

php - 如何在没有构造函数的情况下强制初始化类变量?

javascript - 无法从私有(private)方法访问公共(public)方法

python - 循环生成子图时出错

python - matplotlib 中的多行 x 刻度标签