简体   繁体   中英

The plot appears at wrong position when embedding Vispy SceneCanvas into PyQt5

I am trying to write a subclass of vispy.scene.SceneCanvas and use it as a plot widget in my PyQt5 application. However, the plot always appears at wrong position (top-right corner), and I did not get any hint from vispy docs about this problem.

Actual Position

Expected Position

code:

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_()

I compared your code to what was in the plotting API and was able to get it to work using height_max and width_max instead of stretch . See https://github.com/vispy/vispy/blob/efa49b6896321374149998e15f8bce2ae327ba70/vispy/plot/plotwidget.py#L116-L131

Here is what the code looks like for me now:

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_()

The output ends up looking like:

在此处输入图片说明

The problem is solved after I realize I made a noob mistake, the y-axis stretch should be set as (0.1, 1) instead of (1, 0.1), although it is interesting that it appears correctly if I don't use the canvas as a PyQt widget. @djhoese 's solution also works well.

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_()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM