fig.add_subplot()与pyplot.subplot() [英] figure.add_subplot() vs pyplot.subplot()
问题描述
add_subplot()
和 subplot()
有什么区别?如果没有一个子图,它们似乎都添加了一个子图.我查看了文档,但我无法区分.仅仅是为了使将来的代码更灵活吗?
What is the difference between add_subplot()
and subplot()
? They both seem to add a subplot if one isn't there. I looked at the documentation but I couldn't make out the difference. Is it just for making future code more flexible?
例如:
fig = plt.figure()
ax = fig.add_subplot(111)
对比
plt.figure(1)
plt.subplot(111)
来自matplotlib教程.
from matplotlib tutorials.
推荐答案
如果您需要引用 ax
以备后用:
If you need a reference to ax
for later use:
ax = fig.add_subplot(111)
给你一个:
plt.subplot(111)
您需要执行以下操作:
ax = plt.gca()
同样,如果以后想操作图形:
Likewise, if want to manipulate the figure later:
fig = plt.figure()
立即为您提供参考,而不是:
gives you a reference right away instead of:
fig = plt.gcf()
如果要处理多个图形子图,则获取显式引用会更加有用.比较:
Getting explicit references is even more useful if you work with multiple subplots of figures. Compare:
figures = [plt.figure() for _ in range(5)]
具有:
figures = []
for _ in range(5):
plt.figure()
figures.append(plt.gcf())
这篇关于fig.add_subplot()与pyplot.subplot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!