python - Matplotlib plt.xlabel() 与 ax.set_xlabel()
问题描述
我正在使用一些在 Python 中使用 matplotlib 的单例版本的代码,即它有类似的调用
plt.figure()
...
plt.xlabel("abc")
我正在尝试将其转换为功能/无内存版本:
fig,ax = plt.subplots()
...
ax.set_xlabel("abc")
几个问题:
是否有直接设置轴的 xlabel 的选项?像
ax.xlabel = "xlabel string"
什么?从文档来看,这似乎是不可能的。(甚至不是我们可以设置的私有属性)
还是总是需要通过设置器?这一直让我感到困惑,并让我觉得它不是 Pythonic。
为什么 API 会从
plt.xlabel()
变为ax.set_xlabel()
?
解决方案
Matplotlib 有 pyplot 接口,后来开发了面向对象的接口,如文档(https://matplotlib.org/3.2.1/tutorials/introductory/lifecycle.html)中所述。
现在首选后者,因此使用 fig, ax = plt.subplots 然后在 ax 上使用 setter 方法。
推荐阅读
- python - 在数据工厂中使用 Azure Function APP 运行 Python 脚本
- java - 如何使用相同的按钮启动和停止方法
- json - Elasticsearch:从对象映射的特定对象中获取最大值和最小值
- python - 使用 Pandas 读取深度嵌套的 xml
- sql - 名称已在现有对象 PL/SQL oracle 中使用
- c# - 选择所有文本时的 Xamarin 条目光标位置
- vue.js - 在兄弟组件之间传递事件
- python - 如何为python循环多个附加
- java - 如何使用非实体超类引用 CriteriaQuery 中的子类字段
- node.js - NodeJS 程序从 Wikipedia 中抓取表格列