2016-11-17 32 views
0

我正在開發一個使用Qt Designer和PyQt4的桌面應用程序。我需要顯示一個輪廓圖,在Python中可以用matplotlib.pyplot.contourf來完成。我想在QGraphicView對象中顯示結果。如何整合matplotlib與QGraphicView的組合?

我試圖通過在Qt-Designer中將QGraphicView提升爲pygtgraph來實現。如果QGraphicView的對象名稱是CNOPlot我寫了

import matplotlib.pyplot as plt 
self.CNOPlot.plot(plt.contourf(xx, yy, Z,cmap=plt.cm.autumn, alpha=0.8)) 

它在單獨的窗口中給出輸出。 我想這將被繪製在CNOPlot內。

+0

添加'QGraphicsScene'到您的視圖和渲染場景的情節。 – Chr

+0

非常感謝你@C.Dip。你可以給一些例子或任何鏈接來說明如何做到這一點。 我完全不熟悉它。 –

+0

好的,我寫一個簡短的答案 – Chr

回答

0

下面是一個簡單的例子:

import matplotlib.pyplot as plt 
from PyQt4 import QtGui 
from PyQt4.Qt import Qt 
from matplotlib.figure import Figure 
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 

class MyView(QtGui.QGraphicsView): 
    def __init__(self): 
     QtGui.QGraphicsView.__init__(self) 

     scene = QtGui.QGraphicsScene(self) 
     self.scene = scene 

     figure = Figure() 
     axes = figure.gca() 
     axes.set_title("title") 
     axes.plot(plt.contourf(xx, yy, Z,cmap=plt.cm.autumn, alpha=0.8)) 
     canvas = FigureCanvas(figure) 
     canvas.setGeometry(0, 0, 500, 500) 
     scene.addWidget(canvas) 

     self.setScene(scene) 
+0

非常感謝,但它仍然是一個單獨的窗口,並且具有以下錯誤跟蹤: AttributeError:QuadContourSet實例沒有屬性'__float__' –

+0

'xx'和'yy'必須是整數數組。 – Chr

+0

但根據要求,他們不能。 –