首页 > 解决方案 > Python:如何在函数外使用函数中定义的图形?

问题描述

我有这个代码:

def functionPlot():
    ax = plt.figure().add_subplot(111)
    ax.plot([1,1])
    return ax

if __name__=="__main__":
   ax=functionPlot()     

我想获得在函数“functionPlot”中定义的图形,以便在“main”函数中使用。我无法做到这一点。我怎样才能做到这一点?我应该从函数返回什么?

标签: python-3.xfunctionfigure

解决方案


也许你的意思是:

import matplotlib.pyplot as plt

def functionPlot():
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot([1,1])
    return fig

if __name__=="__main__":
    fig = functionPlot()
    # do other things with fig

推荐阅读