In this article, we will see how we can create a scatter plot graph in the PyQtGraph module which shows text instead of symbols as 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 text as spots is that we will create a text which will act as the spots.
We can create a plot window and create 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)
Approach:
1. Import pyqtgraph, pyqt5 and numpy modules
2. Create Main window class
3. Create a plot window object
4. Create a method for creating label which return the text symbol which is named tuple
5. Create random strings using numpy
6. Create random position using numpy for plotting the strings
7. Create a scatter plot item
8. Create spots using random strings and position and add them to the scatter plot
9. Add the scatter plot graph to the plot window
10. Add plot window and extra label widget to the grid layout of main window
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 ):
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()
# creating a label
label = QLabel( "Geeksforgeeks Scatter Plot Graph with 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()
# text symbols
TextSymbol = namedtuple( "TextSymbol" , "label symbol scale" )
# number of text
n = 150
# method for creating label
def createLabel(label, angle):
# QPainterPath
symbol = QPainterPath()
# creating QFont object
f = QFont()
# setting font size
f.setPointSize( 10 )
# adding text
symbol.addText( 0 , 0 , f, label)
# getting bounding rectangle
br = symbol.boundingRect()
# getting scale
scale = min ( 1. / br.width(), 1. / br.height())
# getting transform object
tr = QTransform()
# setting scale to transform object
tr.scale(scale, scale)
# rotate the transform
tr.rotate(angle)
# translating
tr.translate( - br.x() - br.width() / 2. , - br.y() - br.height() / 2. )
# returning text symbol
return TextSymbol(label, tr. map (symbol), 0.1 / scale)
# creating a random string
def random_str(): return (
' '.join([chr(np.random.randint(ord(' A '), ord(' z')))
for i in range (np.random.randint( 1 , 5 ))]),
np.random.randint( 0 , 360 ))
# plotting the scatter plot
scatter = pg.ScatterPlotItem(size = 10 , pen = pg.mkPen( 'w' ), pxMode = True )
# getting random position
pos = np.random.normal(size = ( 2 , n), scale = 1e - 5 )
# creating spots
spots = [{ 'pos' : pos[:, i], 'data' : 1 , 'brush' : pg.intColor(i, n), 'symbol' : i % 5 , 'size' : 5 + i / 10. } for i
in range (n)]
# adding spots to the scatter plot
scatter.addPoints(spots)
# spots
spots = [{ 'pos' : pos[:, i], 'data' : 1 , 'brush' : pg.intColor(i, n), 'symbol' : label[ 1 ],
'size' : label[ 2 ] * ( 5 + i / 10. )} for (i, label) in
[(i, createLabel( * random_str())) for i in range (n)]]
# adding points 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 :