0

我正在尝试编写 vispy.scene.SceneCanvas 的子类并将其用作我的 PyQt5 应用程序中的绘图小部件。然而,情节总是出现在错误的位置(右上角),我没有从 vispy 文档中得到任何关于这个问题的提示。

实际位置

预期职位

代码:

import vispy.scene as scene
import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys 

class CustomPlot(scene.SceneCanvas):

    def __init__(self, **kwargs):

        super().__init__(**kwargs)

        self.unfreeze()
        self.grid = self.central_widget.add_grid(spacing=0)
        self.vb = self.grid.add_view(row=0, col=1, camera='panzoom')

        self.x_axis = scene.AxisWidget(orientation='bottom')
        self.x_axis.stretch = (1, 0.1)
        self.grid.add_widget(self.x_axis, row=1, col=1)
        self.x_axis.link_view(self.vb)

        self.y_axis = scene.AxisWidget(orientation='left')
        self.y_axis.stretch = (1, 0.1)
        self.grid.add_widget(self.y_axis, row=0, col=0)
        self.y_axis.link_view(self.vb)

        pos = np.array([[0, 0], [1, 1], [2, 0]])
        line = scene.Line(pos, 'red', parent=self.vb.scene)


if __name__ == "__main__":

    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication([])
    win = QMainWindow()
    win.setCentralWidget(CustomPlot().native)
    win.show()

    if (sys.flags.interactive != 1) or not hasattr(Qt.QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec_()
4

2 回答 2

0

我将您的代码与绘图 API 中的代码进行了比较,并且能够使用height_maxandwidth_max而不是stretch. 见https://github.com/vispy/vispy/blob/efa49b6896321374149998e15f8bce2ae327ba70/vispy/plot/plotwidget.py#L116-L131

这是我现在的代码:

import vispy.scene as scene
import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys 

class CustomPlot(scene.SceneCanvas):

    def __init__(self, **kwargs):

        super().__init__(**kwargs)

        self.unfreeze()
        self.grid = self.central_widget.add_grid(spacing=0)
        self.vb = self.grid.add_view(row=0, col=1, camera='panzoom')

        self.x_axis = scene.AxisWidget(orientation='bottom')
        self.grid.add_widget(self.x_axis, row=1, col=1)
        self.x_axis.height_max = 40
        self.x_axis.link_view(self.vb)

        self.y_axis = scene.AxisWidget(orientation='left')
        self.y_axis.width_max = 40
        self.grid.add_widget(self.y_axis, row=0, col=0)
        self.y_axis.link_view(self.vb)

        pos = np.array([[0, 0], [1, 1], [2, 0]])
        line = scene.Line(pos, 'red', parent=self.vb.scene)


if __name__ == "__main__":

    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication([])
    win = QMainWindow()
    win.setCentralWidget(CustomPlot().native)
    win.show()

    if (sys.flags.interactive != 1) or not hasattr(Qt.QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec_()

输出最终看起来像:

在此处输入图像描述

于 2021-11-04T15:12:55.413 回答
0

在我意识到我犯了一个菜鸟错误后问题就解决了,y 轴拉伸应该设置为 (0.1, 1) 而不是 (1, 0.1),尽管有趣的是如果我不使用它会正确显示画布作为 PyQt 小部件。@djhoese 的解决方案也很有效。

import vispy.scene as scene
import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys 

class CustomPlot(scene.SceneCanvas):

    def __init__(self, **kwargs):

        super().__init__(**kwargs)

        self.unfreeze()
        self.grid = self.central_widget.add_grid(spacing=0)
        self.vb = self.grid.add_view(row=0, col=1, camera='panzoom')

        self.x_axis = scene.AxisWidget(orientation='bottom')
        self.x_axis.stretch = (1, 0.1)
        self.grid.add_widget(self.x_axis, row=1, col=1)
        self.x_axis.link_view(self.vb)

        self.y_axis = scene.AxisWidget(orientation='left')
        self.y_axis.stretch = (0.1, 1)
        self.grid.add_widget(self.y_axis, row=0, col=0)
        self.y_axis.link_view(self.vb)

        pos = np.array([[0, 0], [1, 1], [2, 0]])
        line = scene.Line(pos, 'red', parent=self.vb.scene)


if __name__ == "__main__":

    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication([])
    win = QMainWindow()
    win.setCentralWidget(CustomPlot().native)
    win.show()

    if (sys.flags.interactive != 1) or not hasattr(Qt.QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec_()
于 2021-11-05T01:43:40.203 回答