Python Forum
plotting 2d and 3d using pyqt5 and pyqtgraph and GLSurfacePlotItem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
plotting 2d and 3d using pyqt5 and pyqtgraph and GLSurfacePlotItem
#1
Hi, I am trying to plot two graphs in one window, one 2d and another 3d. I am able to get the 2d plot to work but when I try to plot in 3d using GLSurfacePlotItem I get the following error:
Error:
AttributeError: 'GLViewWidget' object has no attribute 'GLSurfacePlotItem'
the 3d plot widget was promoted with class name:GLViewWidget, headerfile: pyqtgraph.opengl.h. how can I plot in 3d using pyqtgraph? I have the code that I want to run working, but it isn't inserted in a pyqt5 window.

this is the code I am using(.py)
from PyQt5 import QtWidgets, uic
#import pyqtgraph.opengl as gl
import sys
import numpy as np

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page
        uic.loadUi('help.ui', self)

        # Set up plots axes
        self.graphWidget_2D.setLabel('left', 'Pixels')
        self.graphWidget_2D.setLabel('bottom', 'Time', 'days')
        
        self.plot()

    def plot(self):
        Z = np.ones((10, 10))
        self.graphWidget_3D.GLSurfacePlotItem(z=Z, shader='shaded', color=(0.5, 0.5, 1, 1))
        a = np.random.randn(150)
        b = np.random.randn(150)
        self.graphWidget_2D.plot(np.sort(a), np.sort(b))


def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':         
    main()
and this is the ui file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1120</width>
    <height>833</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <widget class="PlotWidget" name="graphWidget_2D" native="true"/>
    </item>
    <item row="0" column="1">
     <widget class="GLViewWidget" name="graphWidget_3D" native="true"/>
    </item>
    <item row="1" column="0">
     <spacer name="horizontalSpacer">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>40</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="1" column="1">
     <spacer name="horizontalSpacer_2">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>40</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1120</width>
     <height>26</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="actionb"/>
    <addaction name="actiona"/>
    <addaction name="actionSave"/>
    <addaction name="separator"/>
    <addaction name="actionOptions"/>
    <addaction name="separator"/>
    <addaction name="actionExit"/>
   </widget>
   <addaction name="menuFile"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <action name="actiona">
   <property name="text">
    <string>Open</string>
   </property>
  </action>
  <action name="actionb">
   <property name="text">
    <string>File</string>
   </property>
  </action>
  <action name="actionOptions">
   <property name="text">
    <string>Options</string>
   </property>
  </action>
  <action name="actionSave">
   <property name="text">
    <string>Save</string>
   </property>
  </action>
  <action name="actionExit">
   <property name="text">
    <string>Exit</string>
   </property>
  </action>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QWidget</extends>
   <header>pyqtgraph.h</header>
   <container>1</container>
  </customwidget>
  <customwidget>
   <class>GLViewWidget</class>
   <extends>QWidget</extends>
   <header>pyqtgraph.opengl.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>
Reply
#2
never mind, I solved it

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page
        uic.loadUi('MainWindow.ui', self)

        self.setup2d_graphs()
        self.setup3d_inv()
        
        self.plot([1,2,3,4,5,6,7,8,9,10], [30,32,34,32,33,31,29,32,35,45])

    def setup3d_inv(self):
        self.graphWidget.setCameraPosition(distance=50)
        ## Add a grid to the view
        g = gl.GLGridItem()
        g.scale(1, 1, 1)
        g.setDepthValue(100)  # draw grid after surfaces since they may be translucent
        self.graphWidget.addItem(g)
    
    def plot(self, hour, temperature):
        Z = np.ones((2, 2))
        p1 = gl.GLSurfacePlotItem(z=Z, shader='shaded', color=(0.5, 0.5, 1, 1))
        self.graphWidget.addItem(p1)
        
        # self.graphWidget.plot(hour, temperature)
        a = np.random.randn(150)
        b = np.random.randn(150)
        self.graphWidget_2D.plot          (np.sort(a), np.sort(b))


def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':         
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,785 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020