[英]how to add the matplotlib 3d pyplot in python qt designer or pyqt5
My question is very simple but I can't find the answer, can everyone help me?我的问题很简单,但我找不到答案,大家可以帮帮我吗?
please请
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.backend_qt5agg. 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。
then:然后:
in my concept, matplotlib.backends.backend_qt5agg only draw with 2d axes, and it can't rotate.在我的概念中, matplotlib.backends.backend_qt5agg 只用二维轴绘制,它不能旋转。
how can I put the 3d pyplot axes on pyqt5?如何将 3d pyplot 轴放在 pyqt5 上?
my example is:我的例子是:
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()
in the sample, not what I want.在示例中,不是我想要的。
how can I change the pyplot and setting on pyqt5.如何更改 pyqt5 上的 pyplot 和设置。
Thanks all very much.非常感谢大家。
It is possible to draw 3D axes on a FigureCanvasQTAgg
canvas using a Figure
container.可以使用
Figure
容器在FigureCanvasQTAgg
canvas 上绘制 3D 轴。 One issue with your code is that axes3d.plot
expects 1D arrays for the x, y, and z-coordinates as input parameters but you are providing 2D arrays.您的代码的一个问题是,
axes3d.plot
期望 x、y 和 z 坐标的 1D arrays 作为输入参数,但您提供的是 2D ZA3CBC3F9D0CE2F71C1554E1B67CZD。 To get this code to work, you need to use something like axes3d.plot_surface
or axes3D.plot_wireframe
instead, eg要使此代码正常工作,您需要使用
axes3d.plot_surface
或axes3D.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.