Plotting 2D scatter points in a PyQt5 UI #783
Answered
by
marcomusy
DRLing2021
asked this question in
Q&A
-
Hello, I notice that vedo does fantastic job on 3D plotting. Now we are making a UI based on PyQt5 to show both 3D and 2D scatter points in different tabs respectively. We've got the 3D points plotting idea from the community. However we found no relative topics about 2D scatter plotting. Can anyone help on it? Your advice is deeply appreciated. |
Beta Was this translation helpful? Give feedback.
Answered by
marcomusy
Jan 8, 2023
Replies: 1 comment 3 replies
-
Hi, you can find many examples of scatter plots in import sys
from PyQt5 import QtCore, QtWidgets
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vedo import Mesh, dataurl, Plotter
from vedo.pyplot import np, histogram
try:
_encoding = QtWidgets.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtWidgets.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtWidgets.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tab1 = QtWidgets.QWidget()
self.tab1.setObjectName("tab1")
self.gridLayout1 = QtWidgets.QGridLayout(self.tab1)
self.vtkLayout1 = QtWidgets.QVBoxLayout()
self.vtkLayout1.setObjectName("vtkLayout1")
self.gridLayout1.addLayout(self.vtkLayout1, 0, 0, 1, 1)
self.tab2 = QtWidgets.QWidget()
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.addTab(self.tab1, "tab1")
self.tabWidget.addTab(self.tab2, "tab2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.addWidget(self.tabWidget)
self.gridLayout2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout2.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
MainWindow.setStatusBar(self.statusbar)
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.tabWidget.setTabText(
self.tabWidget.indexOf(self.tab1), _translate("MainWindow", "Tab 1", None)
)
self.tabWidget.setTabText(
self.tabWidget.indexOf(self.tab2), _translate("MainWindow", "Tab 2", None)
)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.vtkWidget1 = QVTKRenderWindowInteractor(self)
self.vtkLayout1.addWidget(self.vtkWidget1)
self.plt1 = Plotter(qt_widget=self.vtkWidget1, axes=1)
self.id2 = self.plt1.add_callback("key press", self.onKeypress)
self.plt1 += Mesh(dataurl+'shark.ply').c('cyan')
self.plt1.show()
self.vtkWidget2 = QVTKRenderWindowInteractor(self)
self.verticalLayout.addWidget(self.vtkWidget2)
self.plt2 = Plotter(qt_widget=self.vtkWidget2)
data = np.random.randn(1000)
self.plt2 += histogram(data, aspect=16/9)
self.plt2.show(zoom=2, mode='image')
def onClose(self):
self.vtkWidget1.close()
def onKeypress(self, evt):
print("You have pressed key:", evt.keypress)
if evt.keypress=='q':
sys.exit(0)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
app.aboutToQuit.connect(window.onClose)
window.show()
sys.exit(app.exec_()) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
marcomusy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, you can find many examples of scatter plots in
examples/pyplot
and inexamples.other/qt_*.py
e.g.: