我正在使用一些在 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/