gpt4 book ai didi

python - PyQt5 嵌入 3D 散点图

转载 作者:行者123 更新时间:2023-12-02 02:22:28 24 4
gpt4 key购买 nike

我尝试将 3D 绘图与 PyQt5 一起使用,但无法使绘图旋转工作。我已经让 matplotlib 工具栏工作了一半,它可以保存绘图并调整其大小。尽管它适用于二维图,但缩放和移动不起作用,不确定散点图是否允许缩放或移动。我需要的是轮换工作。

到目前为止的代码:

import sys
from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication, QSizePolicy, QVBoxLayout, QPushButton
import matplotlib
matplotlib.use("Qt5Agg")

from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

from mpl_toolkits.mplot3d import axes3d, Axes3D



class MyMplCanvas(FigureCanvas):
# Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.fig = fig
self.axes = fig.add_subplot(111, projection='3d')
# self.fig = plt.figure()
# self.axes = Axes3D(self.fig)

self.compute_initial_figure()
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)

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

def compute_initial_figure(self):
pass


class MyStaticMplCanvas(MyMplCanvas):
# Simple canvas with a sine plot.
def compute_initial_figure(self):
# t = arange(0.0, 3.0, 0.01)
# s = sin(2 * pi * t)
# self.axes.plot(t, s)
xs = [1, 2]
ys = [1, 2]
zs = [1, 2]

self.axes.scatter(xs, ys, zs, c='b')

self.axes.mouse_init()


class PlotDialog(QWidget):
def __init__(self):
QWidget.__init__(self)

self.plot_layout = QVBoxLayout(self)
self.plot_canvas = MyStaticMplCanvas(self, width=5, height=4, dpi=100)

self.navi_toolbar = NavigationToolbar(self.plot_canvas, self)
self.plot_layout.addWidget(self.plot_canvas) # the matplotlib canvas
self.plot_layout.addWidget(self.navi_toolbar)


if __name__ == "__main__":
import sys

app = QApplication(sys.argv)
dialog = PlotDialog()
dialog.show()
sys.exit(app.exec_())

最佳答案

您必须在更新几何图形后初始化鼠标,在您的情况下,您在导致失败之前执行这些操作。

# ...
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
<b>self.axes.mouse_init()</b>
# ...

关于python - PyQt5 嵌入 3D 散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59493811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com