Matplotlib plt.xlabel() 与 ax.set_xlabel() [英] Matplotlib plt.xlabel() vs ax.set_xlabel()

查看:728
本文介绍了Matplotlib plt.xlabel() 与 ax.set_xlabel()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using some code that uses the singleton-version of matplotlib in Python, i.e. it has calls like

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

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

I am trying to convert it to the functional/memory-less version:

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

几个问题:

  • 是否可以直接设置坐标轴的 xlabel?某物像 ax.xlabel = "xlabel string"?

从文档来看,似乎是这样是不可能的.(不是甚至是我们可以设置的私有属性)

From the documentation, it seems like this is not possible. (not even a private attribute we can set)

还是总是需要通过二传手?这一直让我感到困惑,并认为我不是 Pythonic.

Or is it always required to go through the setter? This has always confused me and struck me as non-Pythonic.

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

Why did the API change going from plt.xlabel() to ax.set_xlabel()?

推荐答案

Matplotlib具有pyplot接口,然后按照文档(

Matplotlib had the pyplot interface, and then later developed the object-oriented interface, as described in the documentation (https://matplotlib.org/3.2.1/tutorials/introductory/lifecycle.html).

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

The latter is now preferred, so use fig, ax = plt.subplots and then use the setter method on ax.

这篇关于Matplotlib plt.xlabel() 与 ax.set_xlabel()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆