figure.add_subplot() vs pyplot.subplot()

前端 未结 2 1421
天命终不由人
天命终不由人 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条回答
  •  猫巷女王i
    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())
    

提交回复
热议问题