以下是一些代码:
class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=2, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
class MyDynamicMplCanvas(MyMplCanvas):
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
def compute_initial_figure(self):
self.line, = self.axes.plot([],[],color='b')
self.axes.set_title(self.title)
self.axes.set_xlabel("Time")
self.axes.ticklabel_format(style='plain',useOffset = False)
def update_figure(self,voltage):
self.line.set_data(np.linspace(0,len(voltage)-1,num = len(voltage)), voltage)
self.axes.relim()
self.axes.autoscale_view(True,True,True)
self.draw()
class Monitor(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ac_1_graph = MyDynamicMplCanvas(self.ac_graph,width = 10, height =3 , dpi = 60)
基本上,我的代码的最后一行初始化了Canvas并绘制了我的情节。但是我想从这个类中创建几个对象(图),问题是我需要能够更改每个单独图的标题,轴标签。
我尝试在MyDynamicMplCanvas的 init 中定义变量“title”,如下所示:
class MyDynamicMplCanvas(MyMplCanvas):
def __init__(self, title, *args, **kwargs):
...
self.title = title
def compute_initial_figure(self):
...
self.axes.set_title(self.title)
...
class Monitor(QtGui.QMainWindow):
...
self.ac_1_graph = MyDynamicMplCanvas('Title Here!', self.ac_graph,width = 10, height =3 , dpi = 60)
这导致程序无法加载说:
RuntimeError: super-class __init__() of type MyDynamicMplCanvas was never called
所以我想知道是否有人可以给我一些关于如何创建我可以为每个不同对象定义的类变量的指导
答案 0 :(得分:0)
MyDynamicMplCanvas类的init应该调用super:
def __init__(self, title, *args, **kwargs):
super(MyDynamicMp1Canvas, self).__init__(*args, **kwargs)
self.title = title