[英]figure.add_subplot() vs pyplot.subplot()
What is the difference between add_subplot()
and subplot()
? 是什么区别add_subplot()
和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? 它只是为了使未来的代码更灵活吗?
For example: 例如:
fig = plt.figure()
ax = fig.add_subplot(111)
vs VS
plt.figure(1)
plt.subplot(111)
from matplotlib tutorials. 来自matplotlib教程。
If you need a reference to ax
for later use: 如果您需要参考ax
以供日后使用:
ax = fig.add_subplot(111)
gives you one while with: 给你一个时间:
plt.subplot(111)
you would need to do something like: 你需要做类似的事情:
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)]
with: 有:
figures = []
for _ in range(5):
plt.figure()
figures.append(plt.gcf())
pyplot.subplot
is wrapper of Figure.add_subplot
with a difference in behavior. pyplot.subplot
是包装Figure.add_subplot
与行为上的差异。 Creating a subplot with pyplot.subplot
will delete any pre-existing subplot that overlaps with it beyond sharing a boundary. 使用pyplot.subplot
创建子图将删除任何pyplot.subplot
重叠的预先存在的子图,而不是共享边界。 If you do not want this behavior, use the Figure.add_subplot
method or the pyplot.axes
function instead. 如果您不想要此行为,请改用Figure.add_subplot
方法或pyplot.axes
函数。 More 更多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.