When should you use ax. in matplotlib?

别说谁变了你拦得住时间么 提交于 2019-12-11 08:04:48

问题


I still don't understand the best usage of fig, plt. and ax. in matplotlib Should I be using plt. all the time? When should ax. be used?


回答1:


Use fig and ax are part of the object oriented interface and should be used as often as possible.

Using the old plt interface works in most cases too, but when you create and manipulate many figures or axes it can become quite tricky to keep track of all your figures:

plt.figure()
plt.plot(...)      # this implicitly modifies the axis created beforehand
plt.xlim([0, 10])  # this implicitly modifies the axis created beforehand
plt.show()

compared to

fig, ax = plt.subplots(1, 1)
ax.plot(...)           # this explicitly modifies the axis created beforehand
ax.set_xlim([0, 10])   # this explicitly modifies the axis created beforehand
plt.show()


来源:https://stackoverflow.com/questions/46152940/when-should-you-use-ax-in-matplotlib

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