在多个类方法中使用Matplotlib轴

时间:2014-06-24 15:39:43

标签: python matplotlib

我编写了一个脚本来绘制对象的亮度,因为它们在6个滤镜的某些组合中随时间变化。我有原始数据和适合数据,我希望能够切换打开和关闭它们。我为每个对象编写了一个类,然后在该方法中创建a)轴,b)绘制原始数据c)绘制模型拟合。它看起来像:

    class SNPlot(object):
        def __init__(self, params):
            #initialise with various parameters

        def make_axes(self):

            gs = gridspec.GridSpec(3,2)

            #depending on what filters there are make various axes eg
            if ('B' in self.filters):
                ax1 = fig.add_subplot(gs[0,0])

            #create ax1-ax6 if needed for the combination of filters

        def add_data(self):

            if ('B' in self.filters):
                ax1.plot(data[0], data[1], 'bo')

            #etc for all the different filters ax2, ax3, ax4, ax5 and ax6

        def add_model(self):

            if ('B' in self.filters):
                ax1.plot(model[0], model[1], 'b-')

            #etc for all the different filters ax2, ax3, ax4, ax5 and ax6


    def main():

        parser = argparse.ArgumentParser(description='Plot LCs for paper')
        parser.add_argument("-d", "--data", action='store_true', help = 'Plot LC data')
        parser.add_argument('-m', '--model', action='store_true',help='Plot model fits' )

        args = vars(parser.parse_args())

        sn = SNPlot(params)
        sn.make_axes()

        if (args[data] == True): sn.add_data
        if (args[model] == True): sn.add_model

当我使用-d选项尝试此操作时,我收到NameError: global name 'ax1' is not defined的错误。如何在其他类方法中使用ax1-6?

0 个答案:

没有答案