欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

matplotlib嵌入pyqt5绘制2D、3D图

程序员文章站 2022-03-02 08:18:11
...

没多少代码,应该不难看懂

matplot部分

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from mpl_toolkits.mplot3d import Axes3D


class plotCanvas(FigureCanvas):
    def __init__(self, title, parent=None, width=5, height=4, dpi=100,axis=2):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axis = axis
        if axis ==2:
            self.axes = self.fig.add_subplot(111)
        else:
            self.axes = Axes3D(self.fig)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        self.title = title


        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        

    def plot(self, data):
        if self.axis == 2:
            self.axes.scatter(data["x"], data["y"])
            self.axes.set_title(self.title)
        if self.axis == 3:
            self.axes.set_zlabel('Z', fontdict={'size': 15})
            self.axes.set_ylabel('Y', fontdict={'size': 15})
            self.axes.set_xlabel('X', fontdict={'size': 15})
            self.axes.scatter(data['x'], data['y'], data['z'])
        self.draw()

    def clean(self):
        self.axes.cla()

tabWidget部分

class qtMatplot(QWidget):
    def __init__(self, parent):
        super(qtMatplot, self).__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.horizontalLayout)

        # undostack
        self.undoStack = QUndoStack(self)

        # tabWidget
        self.tabWidget = QTabWidget(self)
        self.layout().addWidget(self.tabWidget)
        self.tabWidget.show()

		self.addTab("suface", "simpleExmple",3)
		point = [(1,0,0),(0,1,0),(0,0,1)]
		self.tabWidget.widget(0).plot(np.asarray(point,dtype=[("x","f8"),("y","f8"),("z","f8")]))
	
	def addTab(self, name, title,axis=2):
        widget = plotCanvas(title, self, width=5, height=4,axis=axis)
        self.tabWidget.addTab(widget, name)
相关标签: Qt python python