Friday, December 27, 2024
Google search engine
HomeLanguagesPyQtGraph – Different Colored Spots on Scatter Plot Graph

PyQtGraph – Different Colored Spots on Scatter Plot Graph

In this article, we will see how we can create a scatter plot graph in the PyQtGraph module which have different color spots. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.). A scatter plot (aka scatter chart, scatter graph) uses dots to represent values for two different numeric variables. It is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. The position of each dot on the horizontal and vertical axis indicates values for an individual data point. The main concept of showing different color spots is that we will create spots in the form of a dictionary which will have the color and position property.
We can create a plot window and create a scatter plot graph on it with the help of commands given below 

# creating a pyqtgraph plot window
plt = pg.plot()

# creating a scatter plot graph of size = 10
scatter = pg.ScatterPlotItem(size=10)

In order to do this, we have to do the following 

  1. Import pyqtgraph, pyqt5 and numpy modules
  2. Create Main window class
  3. Create a plot window object
  4. Create a scatter plot item object
  5. Create an empty list for storing each spot
  6. With the help of loops create a spot dictionary that has the keys as ‘pos’, ‘color’, ‘pen’, ‘size’ for the position, color, pen and size of the spot.
  7. Add these spot dictionary to the list
  8. Add the list to the scatter plot item as spots list
  9. Add this scatter plot to the plot window, and further add this plot to the grid layout with other extra widgets like the label

Below is the implementation 

Python3




# importing Qt widgets
from PyQt5.QtWidgets import *
 
# importing system
import sys
 
# importing numpy as np
import numpy as np
 
# importing pyqtgraph as pg
import pyqtgraph as pg
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
from collections import namedtuple
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("PyQtGraph")
 
        # setting geometry
        self.setGeometry(100, 100, 600, 500)
 
        # icon
        icon = QIcon("skin.png")
 
        # setting icon to the window
        self.setWindowIcon(icon)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for components
    def UiComponents(self):
 
        # creating a widget object
        widget = QWidget()
 
        # text
        text = "GeeksforLazyroar Scatter Plot Graph with different color spots"
 
        # creating a label
        label = QLabel(text)
 
        # setting minimum width
        label.setMinimumWidth(130)
 
        # making label do word wrap
        label.setWordWrap(True)
 
        # setting configuration options
        pg.setConfigOptions(antialias = True)
 
        # creating a plot window
        plt = pg.plot()
 
        # creating scatter plot item
        ## Set pxMode=False to allow spots to transform with the view
        scatter = pg.ScatterPlotItem(pxMode = False)
 
        # creating empty list for spots
        spots = []
 
        # creating loop for rows and column
        for i in range(10):
 
            for j in range(10):
 
                # creating  spot position which get updated after each iteration
                # of color which also get updated
                spot_dic = {'pos': (1e-6 * i, 1e-6 * j), 'size': 1e-6,
                            'pen': {'color': 'w', 'width': 2},
                            'brush': pg.intColor(i * 10 + j, 100)}
 
                # adding spot_dic in the list of spots
                spots.append(spot_dic)
 
        # adding spots to the scatter plot
        scatter.addPoints(spots)
 
        # adding scatter plot to the plot window
        plt.addItem(scatter)
 
        # Creating a grid layout
        layout = QGridLayout()
 
        # minimum width value of the label
        label.setMinimumWidth(130)
 
        # setting this layout to the widget
        widget.setLayout(layout)
 
        # adding label in the layout
        layout.addWidget(label, 1, 0)
 
        # plot window goes on right side, spanning 3 rows
        layout.addWidget(plt, 0, 1, 3, 1)
 
        # setting this widget as central widget of the main window
        self.setCentralWidget(widget)
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())


Output : 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments