I have been trying to pass a pyplot Figure out of an external class (as in must be imported), but have been unsuccessful. I don’t even know if this is the way I should be tackling the problem of getting a plot (un-shown) out of a class.
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
So that is the class roughly (names plotfile.py). Here is the bulk other script.
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)
I have tried several variations on this theme and have tried my best googlefu, but not no avail. Any help would be greatly appreciated. If I am way off on my approach to this I am very open to hearing how to do it correctly. Thanks.
A few points: I don’t think
Figureworks like like you think it does, and yourPlotterobject doesn’t have a.show()method, soapp.show(plot)won’t work. The following worked for me: