3

我一直在尝试将 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

这就是大致的类(名称 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)

我已经尝试了这个主题的几种变体,并尝试了我最好的 googlefu,但没有成功。任何帮助将不胜感激。如果我在我的方法上走得很远,我很乐意听到如何正确地做到这一点。谢谢。

4

1 回答 1

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

from plotfile import Plotter

app = Plotter(xval=range(0,10), yval=range(0,10))
plot = app.plotthing()
plot.show()
raw_input()

于 2012-09-27T16:11:12.350 回答