将情节带出课堂

dea*_*ump 2 python matplotlib

我一直试图通过外部类传递pyplot Figure(必须导入),但是没有成功。我什至不知道这是否是我应该解决的方法,以使绘图(未显示)脱离课堂。

from matplotlib.figure import Figure
import matplotlib.pyplot as plt

class Plotter(object):
    def __init__(self, xval=None, yval=None):
        self.xval = xval
        self.yval = yval

    def plotthing(self):
        f = Figure(1)
        sp = f.add_subplot(111)
        sp.plot(self.xval, self.yval, 'o-')
        return f
Run Code Online (Sandbox Code Playgroud)

这就是大致的类(名称为plotfile.py)。这是其他大量脚本。

from plotfile import Plotter
import matplotlib.pyplot as plt

app = Plotter(xval=range(0,10), yval=range(0,10))
plot = app.plotthing()
app.show(plot)
Run Code Online (Sandbox Code Playgroud)

我已经尝试过有关此主题的多种变体,并尝试了我最好的googlefu,但没有成功。任何帮助将不胜感激。如果我对解决这个问题的方法不太满意,那么我很乐意听取如何正确执行此操作。谢谢。

DSM*_*DSM 5

要点:我认为Figure工作原理不像您认为的那样,并且您的Plotter对象没有.show()方法,因此app.show(plot)将无法工作。以下为我工作:


# plotfile.py
import matplotlib.pyplot as plt

class Plotter(object):
    def __init__(self, xval=None, yval=None):
        self.xval = xval
        self.yval = yval

    def plotthing(self):
        f = plt.figure()
        sp = f.add_subplot(111)
        sp.plot(self.xval, self.yval, 'o-')
        return f
Run Code Online (Sandbox Code Playgroud)
from plotfile import Plotter

app = Plotter(xval=range(0,10), yval=range(0,10))
plot = app.plotthing()
plot.show()
raw_input()
Run Code Online (Sandbox Code Playgroud)