How To Draw A Line Graph In Word
The scientific or numerical visualization tasks can be washed in python hands using diverse Python libraries, such as Plotly, Matplotlib, Seaborn, ggplot, PyQt, etc. PyQtGraph is a useful module of the PyQt library to create a graph. This module can create graphs very fast. 2nd and 3D graphics can exist created past using this module. How to apply the PyQtGraph module to draw different types of graphs has shown in this tutorial.
Prerequisites
You lot have to install the PyQtGraph module in your system before creating the graph. Run the following command to install PyQtGraph by using pip3.
$ sudo pip3 install pyqtgraph
Draw Line Graph
A line nautical chart or line graph is a chart that displays information by using a serial of data plots. PlotWidget widget is used to create all plots of PyQtGraph. This widget contains a canvas on which any plot tin can be added or removed.
Example-1: Draw a simple line graph
The following script shows the way to depict a line graph using random numbers. range() function is used to generate 10 sequential numbers that are used for ten-axis values. The random module of python is used to generate 10 random integer numbers used for y-centrality values. plot() method is used to draw each plot of the line graph based on the values of the x-axis and y-axis. When the code is executed in interactive mode, then sys.flags.interactive flag volition be Truthful. If this flag's value is not True, then information technology indicates that the Qt event loop does non require to run considering the REPL (Run, Evaluate, Print, Loop) already has an effect loop.
# Import random module
# Import random module
import random as rd
# Import PyQtGraph Module
import pyqtgraph as pg
# Import QtGui from the PyQtGraph Module
from pyqtgraph.Qt import QtGui
# Generate x-axis values
x = range ( 0 , 10 )
# Generate y-centrality values
y = [ ]
for i in range ( 0 , ten ):
n = rd.randint ( 0 , xx )
y.append (north)
# Initialize the plot
plt = pg.plot ( )
# Set the label for x axis
plt.setLabel ( 'bottom' , 'Sequentially Generated Values' )
# Set the label for y-centrality
plt.setLabel ( 'left' , 'Randomly Generated Values' )
# Prepare horizontal range
plt.setXRange ( 0 , 10 )
# Set vertical range
plt.setYRange ( 0 , 20 )
# Set the title of the graph
plt.setTitle ( "Simple Line Graph" )
# Set the plot values
line = plt.plot (x, y)
# Principal method
if __name__ == '__main__':
# Import sys module
import sys
# Start Qt issue loop unless running in interactive mode
if sys.flags.interactive != 1:
QtGui.QApplication.example ( ).exec ( )
Output:
The post-obit similar line graph will be appeared after executing the above script. The graph will vary in each execution for the random values.
Instance-ii: Draw a Line graph with styling and marker
The following script shows how to describe a stylist line graph based on the values of a python list. mkPen() function is used to set the color and width of the line. 'o' is used as the marker in the graph, and the colour and width of the marker are set past using symbolPen and symbolSize backdrop.
# Import PyQtGraph Module
# Import PyQtGraph Module
import pyqtgraph equally pg
# Import QtGui from the PyQtGraph Module
from pyqtgraph.Qt import QtGui
# Generate x-axis values
ten = range ( 0 , ten )
# Generate y-axis values
y = [ three , vii , 5 , xi , eight , thirteen , nine , sixteen , 15 , 12 ]
# Initialize the plot
plt = pg.plot ( )
# Set the label for x axis
plt.setLabel ( 'bottom' , '10-axis Values' )
# Set the label for y-centrality
plt.setLabel ( 'left' , 'Y-axis Values' )
# Set horizontal range
plt.setXRange ( 0 , x )
# Prepare vertical range
plt.setYRange ( 0 , 20 )
# Ready the championship of the graph
plt.setTitle ( "Line Graph with styling and mark" )
# Set the background color
plt.setBackground ( 'd5ecf2' )
# Prepare the plot values with pen colour and width
line = plt.plot (x, y, pen=pg.mkPen ( 'r' , width= 6 ) , symbol = 'o' , symbolPen= 'b' , symbolSize= 20 )
# Add legend
plt.addLegend ( )
# Show grids
plt.showGrid (ten= True , y= True )
# Main method
if __name__ == '__main__':
# Import sys module
import sys
# Start Qt outcome loop unless running in interactive mode
if sys.flags.interactive != 1:
QtGui.QApplication.instance ( ).exec_ ( )
# Main method
if __name__ == '__main__':
# Import sys module
import sys
# Start Qt event loop unless running in interactive manner
if sys.flags.interactive != 1:
QtGui.QApplication.example ( ).exec_ ( )
Output:
The following line graph will exist appeared after executing the higher up script.
Draw Bar Graph
A set of data can be visualized past using a bar graph. A simple bar creates past comparing the listing of information with a related range of data. BarGraphItem class is used to create the bar graph by using PyQtGraph.
Example-3: Draw a bar graph
The following script shows how to draw a bar graph based on the values of a NumPy array. BarGraphItem is used to draw a bar graph with bluish color and a width value of 0.5.
# Import PyQtGraph module
# Import PyQtGraph module
import pyqtgraph as pg
# Import QtGui from the PyQtGraph Module
from pyqtgraph.Qt import QtGui
# Import NumPy Library
import numpy as np
# Generate x-axis
x = np.arange ( 0 , 10 )
# Generate y-axis
y = np.array ( [ three , v , seven , 9 , 5 , 8 , 10 , six , five , 2 ] )
# Gear up bar chart values and castor color and width
bg = pg.BarGraphItem (10=x, peak=y, width= 0.5 , castor= 'b' )
# Define the plot variable
plt = pg.plot ( )
# Set the label for ten centrality
plt.setLabel ( 'bottom' , '<p style="font-size:20px;color:light-green">10-axis Values</p>' )
# Prepare the label for y-axis
plt.setLabel ( 'left' , '<p mode="font-size:20px;colour:green">Y-axis Values</p>' )
# Set the bar championship
plt.setTitle ( '<h3>Bar Graph using PyQtGraph</h3>' )
# Add the bar chart items
plt.addItem (bg)
# Main method
if __name__ == '__main__':
# Import sys module
import sys
# Starting time Qt event loop unless running in interactive style
if sys.flags.interactive != one:
QtGui.QApplication.instance ( ).exec_ ( )
Output:
The post-obit bar graph will be appeared after executing the above script.
Conclusion
Creating 3 different types of graphs with random values, list items, and NumPy array have shown in this tutorial past using three simple examples. The readers will be able to use the PyQtGraph module for implementing line and bar graphs later on reading this tutorial.
Source: https://linuxhint.com/use-pyqtgraph/
Posted by: lujancoldingaze.blogspot.com

0 Response to "How To Draw A Line Graph In Word"
Post a Comment