fig.add_subplot()与pyplot.subplot() [英] figure.add_subplot() vs pyplot.subplot()

查看:532
本文介绍了fig.add_subplot()与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆