Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

PyQtGraph – Different Colored Spots on Scatter Plot Graph

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 


# 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):
        # setting title
        # setting geometry
        self.setGeometry(100, 100, 600, 500)
        # icon
        icon = QIcon("skin.png")
        # setting icon to the window
        # calling method
        # showing all the widgets
    # method for components
    def UiComponents(self):
        # creating a widget object
        widget = QWidget()
        # text
        text = "Geeksforgeeks Scatter Plot Graph with different color spots"
        # creating a label
        label = QLabel(text)
        # setting minimum width
        # making label do word wrap
        # 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
        # adding spots to the scatter plot
        # adding scatter plot to the plot window
        # Creating a grid layout
        layout = QGridLayout()
        # minimum width value of the label
        # setting this layout to the widget
        # 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
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output : 

My Personal Notes arrow_drop_up
Last Updated : 18 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials