
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/scene/realtime_data/ex01_embedded_vispy.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_scene_realtime_data_ex01_embedded_vispy.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_scene_realtime_data_ex01_embedded_vispy.py:


Embed VisPy into Qt
===================

Display VisPy visualizations in a PyQt5 application.

.. GENERATED FROM PYTHON SOURCE LINES 14-105



.. image-sg:: /gallery/scene/realtime_data/images/sphx_glr_ex01_embedded_vispy_001.png
   :alt: ex01 embedded vispy
   :srcset: /gallery/scene/realtime_data/images/sphx_glr_ex01_embedded_vispy_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    from PyQt5 import QtWidgets

    from vispy.scene import SceneCanvas, visuals
    from vispy.app import use_app

    IMAGE_SHAPE = (600, 800)  # (height, width)
    CANVAS_SIZE = (800, 600)  # (width, height)
    NUM_LINE_POINTS = 200


    class MyMainWindow(QtWidgets.QMainWindow):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

            central_widget = QtWidgets.QWidget()
            main_layout = QtWidgets.QHBoxLayout()

            self._controls = Controls()
            main_layout.addWidget(self._controls)
            self._canvas_wrapper = CanvasWrapper()
            main_layout.addWidget(self._canvas_wrapper.canvas.native)

            central_widget.setLayout(main_layout)
            self.setCentralWidget(central_widget)


    class Controls(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            layout = QtWidgets.QVBoxLayout()
            self.colormap_label = QtWidgets.QLabel("Image Colormap:")
            layout.addWidget(self.colormap_label)
            self.colormap_chooser = QtWidgets.QComboBox()
            self.colormap_chooser.addItems(["viridis", "reds", "blues"])
            layout.addWidget(self.colormap_chooser)

            self.line_color_label = QtWidgets.QLabel("Line color:")
            layout.addWidget(self.line_color_label)
            self.line_color_chooser = QtWidgets.QComboBox()
            self.line_color_chooser.addItems(["black", "red", "blue"])
            layout.addWidget(self.line_color_chooser)

            layout.addStretch(1)
            self.setLayout(layout)


    class CanvasWrapper:
        def __init__(self):
            self.canvas = SceneCanvas(size=CANVAS_SIZE)
            self.grid = self.canvas.central_widget.add_grid()

            self.view_top = self.grid.add_view(0, 0, bgcolor='cyan')
            image_data = _generate_random_image_data(IMAGE_SHAPE)
            self.image = visuals.Image(
                image_data,
                texture_format="auto",
                cmap="viridis",
                parent=self.view_top.scene,
            )
            self.view_top.camera = "panzoom"
            self.view_top.camera.set_range(x=(0, IMAGE_SHAPE[1]), y=(0, IMAGE_SHAPE[0]), margin=0)

            self.view_bot = self.grid.add_view(1, 0, bgcolor='#c0c0c0')
            line_data = _generate_random_line_positions(NUM_LINE_POINTS)
            self.line = visuals.Line(line_data, parent=self.view_bot.scene, color='black')
            self.view_bot.camera = "panzoom"
            self.view_bot.camera.set_range(x=(0, NUM_LINE_POINTS), y=(0, 1))


    def _generate_random_image_data(shape, dtype=np.float32):
        rng = np.random.default_rng()
        data = rng.random(shape, dtype=dtype)
        return data


    def _generate_random_line_positions(num_points, dtype=np.float32):
        rng = np.random.default_rng()
        pos = np.empty((num_points, 2), dtype=np.float32)
        pos[:, 0] = np.arange(num_points)
        pos[:, 1] = rng.random((num_points,), dtype=dtype)
        return pos


    if __name__ == "__main__":
        app = use_app("pyqt5")
        app.create()
        win = MyMainWindow()
        win.show()
        app.run()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 3.317 seconds)


.. _sphx_glr_download_gallery_scene_realtime_data_ex01_embedded_vispy.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: ex01_embedded_vispy.ipynb <ex01_embedded_vispy.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: ex01_embedded_vispy.py <ex01_embedded_vispy.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: ex01_embedded_vispy.zip <ex01_embedded_vispy.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
