问题
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()
toax.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