TypeError:__ init __()缺少1个必需的位置参数:'figure'

时间:2017-05-30 09:33:10

标签: python qt matplotlib pyqt plt

当我尝试通过以下代码绘制交互式绘图时:

import matplotlib.pyplot as plt
import PyQt5
%matplotlib qt
...
plt.plot(a_list,b_list)
plt.show()

我收到以下错误消息:

 --------------------------------------------------------------------------- TypeError                                 Traceback (most recent call
 last) <ipython-input-49-187dd4fead7a> in <module>()
 ----> 1 plt.plot(parameters['eng_list'],np.real(conductivity))
       2 plt.plot(parameters['eng_list'],np.imag(conductivity))
       3 plt.show()

 C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args,
 **kwargs)    3305 @_autogen_docstring(Axes.plot)    3306 def plot(*args, **kwargs):
 -> 3307     ax = gca()    3308     # Deprecated: allow callers to override the hold state    3309     # by passing hold=True|False

 C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in gca(**kwargs)
     948     matplotlib.figure.Figure.gca : The figure's gca method.
     949     """
 --> 950     return gcf().gca(**kwargs)
     951 
     952 # More ways of creating axes:

 C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in gcf()
     584         return figManager.canvas.figure
     585     else:
 --> 586         return figure()
     587 
     588 

 C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in figure(num,
 figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
     533                                         frameon=frameon,
     534                                         FigureClass=FigureClass,
 --> 535                                         **kwargs)
     536 
     537         if figLabel:

 C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt4agg.py
 in new_figure_manager(num, *args, **kwargs)
      44     FigureClass = kwargs.pop('FigureClass', Figure)
      45     thisFig = FigureClass(*args, **kwargs)
 ---> 46     return new_figure_manager_given_figure(num, thisFig)
      47 
      48 

 C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt4agg.py
 in new_figure_manager_given_figure(num, figure)
      51     Create a new figure manager instance for the given figure.
      52     """
 ---> 53     canvas = FigureCanvasQTAgg(figure)
      54     return FigureManagerQT(canvas, num)
      55 

 C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt4agg.py
 in __init__(self, figure)
      74         if DEBUG:
      75             print('FigureCanvasQtAgg: ', figure)
 ---> 76         FigureCanvasQT.__init__(self, figure)
      77         FigureCanvasQTAggBase.__init__(self, figure)
      78         FigureCanvasAgg.__init__(self, figure)

 C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt4.py in
 __init__(self, figure)
      64 
      65         # Note different super-calling style to backend_qt5
 ---> 66         QtWidgets.QWidget.__init__(self)
      67         FigureCanvasBase.__init__(self, figure)
      68         self.figure = figure

 TypeError: __init__() missing 1 required positional argument: 'figure'

我在Windows电脑上使用jupyter笔记本。使用%matplotlib inline,一切正常并生成内嵌图片,但使用%matplotlib qt,则不是。

1 个答案:

答案 0 :(得分:3)

似乎是qt4后端matplotlib中的错误。鉴于您的代码,您似乎有qt5左右,因此您只需使用:

%matplotlib qt5