figure.add_subplot() vs pyplot.subplot()

前端 未结 2 1415
天命终不由人
天命终不由人 2020-12-30 04:51

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

相关标签:
2条回答
  • 2020-12-30 05:39

    If you need a reference to ax for later use:

    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())
    
    0 讨论(0)
  • 2020-12-30 05:43

    pyplot.subplot is wrapper of Figure.add_subplot with a difference in behavior. Creating a subplot with pyplot.subplot will delete any pre-existing subplot that overlaps with it beyond sharing a boundary. If you do not want this behavior, use the Figure.add_subplot method or the pyplot.axes function instead. More

    0 讨论(0)
提交回复
热议问题