Matplotlib plt.xlabel() vs ax.set_xlabel()

懵懂的女人 提交于 2021-01-19 08:23:16

问题


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")

A couple questions:

  • Is there an option to set the xlabel of an axes directly? Something like ax.xlabel = "xlabel string"?

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

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

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


回答1:


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).

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



来源:https://stackoverflow.com/questions/61035292/matplotlib-plt-xlabel-vs-ax-set-xlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!