我正在尝试在Qt4 GUI中嵌入3D图(具有其他按钮),可以使用matplotlib的mplot3d吗?
我知道可以使用“ matplotlib.backends.backend_qt4agg”进行二维绘图,而我已经做到了.
如果是,请举一个例子.
如果没有,最喜欢的选项是什么? VTK?
谢谢!
解决方法:
虽然这不是您将要看到的最干净的东西.我注意到这不是tkinter后端那么快.然后再次起作用.
玩得开心:
import os
import sys
from PyQt4 import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
class QtMplCanvas(FigureCanvas):
def __init__(self, parent=None, width = 6.5, height = 5.5, dpi = 100, sharex = None, sharey = None, fig = None):
if fig == None:
self.fig = Figure(figsize = (width, height), dpi=dpi, facecolor = '#FFFFFF')
self.ax = self.fig.add_subplot(111, projection='3d')
self.fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9)
self.ax.hold(True)
else:
self.fig = fig
FigureCanvas.__init__(self, self.fig)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def sizeHint(self):
w, h = self.get_width_height()
return QtCore.QSize(w, h)
def minimumSizeHint(self):
return QtCore.QSize(10, 10)
def sizeHint(self):
w, h = self.get_width_height()
return QtCore.QSize(w, h)
def minimumSizeHint(self):
return QtCore.QSize(10, 10)
class MyNavigationToolbar(NavigationToolbar) :
def __init__(self, parent, canvas, direction = 'h' ) :
self.canvas = canvas
QWidget.__init__( self, parent )
if direction=='h' :
self.layout = QHBoxLayout( self )
else :
self.layout = QVBoxLayout( self )
self.layout.setMargin( 2 )
self.layout.setSpacing( 0 )
NavigationToolbar2.__init__( self, canvas )
def set_message( self, s ):
pass
class MPL_WIDGET_3D(QtGui.QWidget):
def __init__(self, parent = None, enableAutoScale = False, enableCSV = False, enableEdit = False, fig = None):
QtGui.QWidget.__init__(self, parent)
self.canvas = QtMplCanvas(fig)
self.canvas.ax.mouse_init()
self.toolbar = NavigationToolbar(self.canvas, self.canvas)
self.vbox = QtGui.QVBoxLayout()
self.vbox.addWidget(self.canvas)
self.vbox.addWidget(self.toolbar)
self.setLayout(self.vbox)
###########SAVING FIGURE TO CLIPBOARD##########
self.cb = None #will be used for the clipboard
self.tempPath = getHomeDir()
self.tempPath = os.path.join(self.tempPath,'tempMPL.png')
self.mpl2ClipAction = QtGui.QAction("Save to Clipboard", self)
self.mpl2ClipAction.setShortcut("Ctrl+C")
self.addAction(self.mpl2ClipAction)
QtCore.QObject.connect(self.mpl2ClipAction,QtCore.SIGNAL("triggered()"), self.mpl2Clip)
def mpl2Clip(self):
try:
self.canvas.fig.savefig(self.tempPath)
tempImg = QtGui.QImage(self.tempPath)
self.cb = QtGui.QApplication.clipboard()
self.cb.setImage(tempImg)
except:
print 'Error copying figure to clipboard'
errorMsg = "Sorry: %s\n\n:%s\n"%(sys.exc_type, sys.exc_value)
print errorMsg
####USED TO GET THE USERS HOME DIRECTORY FOR USE OF A TEMP FILE
def valid(path):
if path and os.path.isdir(path):
return True
return False
def env(name):
return os.environ.get( name, '' )
def getHomeDir():
if sys.platform != 'win32':
return os.path.expanduser( '~' )
homeDir = env( 'USERPROFILE' )
if not valid(homeDir):
homeDir = env( 'HOME' )
if not valid(homeDir) :
homeDir = '%s%s' % (env('HOMEDRIVE'),env('HOMEPATH'))
if not valid(homeDir) :
homeDir = env( 'SYSTEMDRIVE' )
if homeDir and (not homeDir.endswith('\\')) :
homeDir += '\\'
if not valid(homeDir) :
homeDir = 'C:\\'
return homeDir
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mplQt = MPL_WIDGET_3D()
mplQt.show()
sys.exit(app.exec_())