簡體   English   中英

how to add the matplotlib 3d pyplot in python qt designer or pyqt5

[英]how to add the matplotlib 3d pyplot in python qt designer or pyqt5

我的問題很簡單,但我找不到答案,大家可以幫幫我嗎?

the question is in python GUI pyqt5, I want to draw the 3d axes using matplotlib , I find the function matplotlib.pyplot, and mpl_toolkits.mplot3d can do it well, but pyqt5 only can use the function matplotlib.figure with matplotlib.backends.后端_qt5agg。

然后:

在我的概念中, matplotlib.backends.backend_qt5agg 只用二維軸繪制,它不能旋轉。

如何將 3d pyplot 軸放在 pyqt5 上?

我的例子是:

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

def spRDM_frame(self):
    fig = Figure()

    # note spRDM_Widget is the widget object on qt designer.
    self.ui.spRDM_Widget.canvas = FigureCanvas(fig)
    self.ui.spRDM_Widget.canvas.axes = self.ui.spRDM_Widget.canvas.figure.add_subplot(1,1,1, projection='3d')
    
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X ** 2 + Y ** 2)
    Z = np.sin(R)

    self.ui.spRDM_Widget.canvas.axes.plot(X,Y,Z)
    self.ui.spRDM_Widget.canvas.draw()

在示例中,不是我想要的。

如何更改 pyqt5 上的 pyplot 和設置。

非常感謝大家。

可以使用Figure容器在FigureCanvasQTAgg canvas 上繪制 3D 軸。 您的代碼的一個問題是, axes3d.plot期望 x、y 和 z 坐標的 1D arrays 作為輸入參數,但您提供的是 2D ZA3CBC3F9D0CE2F71C1554E1B67CZD。 要使此代碼正常工作,您需要使用axes3d.plot_surfaceaxes3D.plot_wireframe之類的東西,例如

from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np

class Widget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.fig = Figure()
        self.canvas = FigureCanvas(self.fig)
        self.axes = self.fig.add_subplot(111, projection='3d')

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.canvas)

        X = np.arange(-5, 5, 0.25)
        Y = np.arange(-5, 5, 0.25)
        X, Y = np.meshgrid(X, Y)
        R = np.sqrt(X ** 2 + Y ** 2)
        Z = np.sin(R)

        self.axes.plot_surface(X, Y, Z)


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    win = Widget()
    win.show()
    app.exec()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM