I am working on my first Python app using PythonXY (2.6 and Qt4). I have a window with some buttons and a QtabWidg开发者_开发问答et. When I press a button I want to plot a 3D image on a specific tab. I managed to get a 2D plot but I can't get the axes to 3D. Via Qt desiger I use the mplwidget. In many examples I see the use of figure(), but I can't get that to work with the mplwidget. (I am not sure if that is even possible)
Can you please show me an example of code defining the 3D axes (in a QtabWidget) with a simple plot ?
Thanks very much
In the code below you should find what you are looking for (at least with matplotlib 1.0.1 and pyqt 4.8):
class MplPlot3dCanvas(FigureCanvas):
def __init__(self):
self.surfs = [] # [{"xx":,"yy:","val:"}]
self.fig = Figure()
self.fig.suptitle("this is the figure title", fontsize=12)
FigureCanvas.__init__(self, self.fig)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.ax = Axes3D(self.fig) # Canvas figure must be created for mouse rotation
self.ax.set_xlabel('row (m CCD)')
self.ax.set_ylabel('col (m CCD)')
self.ax.set_zlabel('Phi (m)')
self.format_coord_org = self.ax.format_coord
self.ax.format_coord = self.report_pixel
def report_pixel(self, xd, yd):
s = self.format_coord_org(xd, yd)
s = s.replace(",", " ")
return s
def update_view(self):
for plt in self.plots:
# del plt
self.ax.collections.remove(plt)
self.plots = []
for surf in self.surfs:
plt = self.ax.plot_surface(surf["xx"], surf["yy"], surf["val"], rstride=5, cstride=5, cmap=cm.jet, linewidth=1, antialiased=True)
self.plots.append(plt)
self.draw()
class MplPlot3dView(QWidget):
def __init__(self, parent = None):
super(MplPlot3dView, self).__init__(parent)
self.canvas = MplPlot3dCanvas()
self.toolbar = NavigationToolbar(self.canvas, self.canvas)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.canvas)
self.vbox.addWidget(self.toolbar)
self.setLayout(self.vbox)
self.to_update = False
Then, you just have to add a QWidget to your QtaWidget and promote it to the new class MplPlot3dView (you will find easily resource on the net about how to do that).
精彩评论