Python Matplotlib图形作为新的类属性。以后如何按命令显示?

Python Matplotlib图形作为新的类属性。以后如何按命令显示?,python,oop,python-3.x,matplotlib,Python,Oop,Python 3.x,Matplotlib,我正在使用Python3分析实验数据。为此,我使用load和fit方法创建了一个数据类,我想完成的是这两个方法都定义或重新定义了属性Data.figure,在运行之后,我希望能够访问该属性并绘制该图 因此,我不知道如何在运行这些方法时创建绘图,但在创建过程中不显示它,而是在访问figure属性时,在命令下显示它 我的代码不工作,看起来像这样 import numpy as np import matplotlib.pyplot as plt from scipy.optimize import

我正在使用Python3分析实验数据。为此,我使用load和fit方法创建了一个数据类,我想完成的是这两个方法都定义或重新定义了属性Data.figure,在运行之后,我希望能够访问该属性并绘制该图

因此,我不知道如何在运行这些方法时创建绘图,但在创建过程中不显示它,而是在访问figure属性时,在命令下显示它

我的代码不工作,看起来像这样

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def expo(x, A, inv_tau):
    return A * np.exp(-inv_tau * x)

class Data:

    def load(self, file_name, parameter, bins=50):
        """Data loading"""

        self.file_name = file_name
        self.parameter = parameter
        dt = np.dtype([(self.parameter, '<f4'), ('molecules', '<f4')])
        self.table = np.fromfile(file_name, dtype=dt)

        # HISTOGRAM CONSTRUCTION
        self.mean = np.mean(self.table[self.parameter])
        self.hist, bin_edges = np.histogram(self.table[self.parameter],
                                            bins=bins,
                                            range=(0, bins * self.mean / 10))
        self.bin_centres = (bin_edges[:-1] + bin_edges[1:]) / 2
        self.bin_width = bin_edges[1] - bin_edges[0]

    def plot(self):
        """Data plotting"""

        # PLOT THE HISTOGRAM
        fig = plt.figure()
        plt.bar(self.bin_centres, self.hist, self.bin_width)
        self.figure = fig

    def fit(self, fit_start=0):
        """Histogram fitting"""

        self.fit_guess = [self.hist[0], 1 / self.mean]
        self.fit_par, self.fit_var = curve_fit(expo,
                                               self.bin_centres[fit_start:-1],
                                               self.hist[fit_start:-1],
                                               p0=self.fit_guess)
        self.tau = 1 / self.fit_par[1]

        # PLOT
        fig = plt.figure()
        hist_fit = expo(self.bin_centres, *self.fit_par)
        plt.bar(self.bin_centres, self.hist, self.bin_width)
        plt.plot(self.bin_centres[fit_start:-1], hist_fit[fit_start:-1])
        self.figure = fig

if __name__ == "__main__":

    data1.load(os.path.join(dirname, file_list[i]))
    data1.fit()
    print(data1.tau)

    data1.figure
这段代码有两个不想要的结果:fit方法返回一个我不想要的图,最后一行“data1.figure”没有得到图,它什么也不做

我做错了什么

独立于问题的解决,你认为有更好的方法来做我想做的吗

谢谢

在绘图中定义self.fig和self.ax:

将用于拟合数据的代码与打印代码分开:

def fit(self, fit_start=0):
    """Histogram fitting"""

    self.fit_guess = [self.hist[0], 1 / self.mean]
    self.fit_par, self.fit_var = curve_fit(expo,
                                           self.bin_centres[fit_start:-1],
                                           self.hist[fit_start:-1],
                                           p0=self.fit_guess)
    self.tau = 1 / self.fit_par[1]
然后调用plot方法:

if __name__ == "__main__":

    data1.load(os.path.join(dirname, file_list[i]))
    data1.fit()
    print(data1.tau)
    data1.plot()
    data1.figure
fig, ax = plt.subplots()
如果要重复使用同一轴进行更多打印,请在打印方法之外定义一次轴:

if __name__ == "__main__":

    data1.load(os.path.join(dirname, file_list[i]))
    data1.fit()
    print(data1.tau)
    data1.plot()
    data1.figure
fig, ax = plt.subplots()
并将ax作为参数传递给plot方法:

def plot(self, ax):
    hist_fit = expo(self.bin_centres, *self.fit_par)
    ...    

fig, ax = plt.subplots()
data1.plot(ax)