Header Ads Widget

Responsive Advertisement

Data visualization important topics and program code


Data Visualisation: - Data visualisation basically refers to the graphical or visual representation of information and data using visual elements like charts, graphs and maps etc. 

Using Pyplot of MATPLOT LIBRARY:- Pyplot is a collection of methods within MATPLOT library of Python which allows user to construct 2D plots (bar, line histogram pie chart) easily and interactively.

firstly we need to install Matplotlib liberary to python console. To INSTALLING AND IMPORTING MATPLOT LIBRARY  We needs one DOS command  

At first you have to open Command Prompt Then write this code – pip install matplotlib 

After doing this process, Now you have to import MATPLOT library. During this process you need active internet connection to your PC

As stated earlier make sure to import matplotlib.pyplot library interface and other required libraries before you start using any of their functions. 


Let us know about various parts of a Chart:-

  • Figure: - Pyplot by default plots every chart into an area called Figure. A figure contains other elements of the plot in it.
  • Axes: - The axes define the area (mostly rectangular in shape for simple plots) on which actual plot (line or bar or graph etc.) will appear. Axes have properties like label, limits and tick marks on them.
  • There are two axes in a plot: (i) X-axis, the horizontal axis, (ii) Y-axis, the vertical axis.
  • Axis label: It defines the name for an axis. It is individually defined tor X-axis and Y-axis each.
  • Limits: These define the range of values and number of values marked on X-axis and Y-axis.
  • Tick_Marks. The tick marks are individual points marked on the X-axis or Y-axis.
  • Title: - This is the text that appears on the top of the plot. It defines what the chart is about.
  • Legends: - These are the different colors that identify different sets of data plotted on the plot. The legends are shown in a corner of the plot.






import matplotlib.pyplot as plt

x = [1,2,3]
y = [5,7,4]

x2 = [1,2,3]
y2 = [10,14,12]
plt.plot(x, y, label='First Line')
plt.plot(x2, y2, label='Second Line')
#plt.xlabel('Plot Number')
#plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
#loc=upper right
#loc=upper left
#loc=lower right
#loc=lower left

OUtput of this code

Line Chart: - A line chart or line graph is a type of chart which displays information as a series of data points called 'marker' connected by straight line segments. The PyPlot interface offers plot() function for creating a line graph.

import matplotlib.pyplot as plt

a = [1,2,3,4,5,6]

b = [2, 4, 6,4,8,9]

plt.plot(b, a)

plt.show()  # this very important function to show graph on screen.

Changing Line Style, Line Width and Line Color in a Line chart:- To change line Color: - You can specify the color code next to data being plotting in plot () function as shown below: 

Syntax: < matplotlib.pyplot >.plot (<data1>, <data2>, <color code>,linewidth =<width>,linestyle = ['solid' | 'dashed', 'dashdot', 'dotted'] ,marker = <valid marker type >, marker size =<in points>, markercolor =<valid color>

if you do not specify the linestyle separately along with linecolor-and-markerstyle-combination-string   (eg, 'rd'), Python will only the markers and not the line. To get the line, specify linestyle argument.

                           "rd"   red colors markers but no line between them.




Some different Colors codes: - 

Character        Color 

'r'                        red 

'b'                       blue 

'g'                       green 

'm'                      magenta

'y'                        yellow 

'k'                        black 

'c'                        cyan 

'w'                       White 

In addition, you can also specify Colors in many other ways, including full Color names ('red', 'green' ,etc.), hex string ('#008000')



Bar Graph

A bar graph / chart is a graphical display of data using bars of different heights. A bar chart can be drawn vertically or horizontally using bars of different heights/widths Pyplot offers bar () function to create a bar chart.  

import matplotlib.pyplot as pl 

a = [1, 2, 3] 

b = [2, 4, 6] 

pl.bar (a, b) 

pl.show( )


You can specify width argument having a sequence (such as lists ) containing widths for each of the bars in the bar() function asYou can specify color argument having a valid color code /name  same as the plot function color code or color name in the bar() function. You can specify color argument having a sequence (such as lists ) containing Colors for each of the bars, in the bar() function. Please note that the width/ color sequence must have widths for all the bars otherwise Python will report an error 

Syntax <matplotlib.pyplot>.bar (<x-sequence>, <y-sequence>, width=<float value>| <width values sequence>,color = <color code/ name>| <color name/ codes sequence>)

bar([1,2,3],[4,5,6], width=(.2,.3,.3), color = ['r''b''g'], align="edge")


Show multiple bar graph on single graph by default this is vertical bar graph 
import matplotlib.pyplot as pl
a = [102030]
b = [61220]
x = [1,2,3]
pl.bar (x, a,color = 'r' , width = 0.35 , align="edge")
pl.bar (x, b, color= 'k', width = 0.35, align="center")
pl.show( )

This graph show overlapping to avoid over lapping we use arange() function as shown in program
just follow the code to plot multiple bar on single Bar graph.

import matplotlib.pyplot as pl
import numpy as np

a = [102030]
b = [61220]
x = np.arange(len(a))
pl.bar (x, a,color = 'r' , width = 0.25,align="edge")
pl.bar (x+0.25, b, color= 'k', width = 0.25, align="edge")
pl.bar (x+0.50, b, color= 'green', width = 0.25, align="edge")
pl.show( )



Here we also discussed about the horizontal bar graph all attributes are same as bar() here we use barh() instead of bar() to plot horizontal bar graph
In bar() we use first list as x axis but in horizontal bar graph we use first y component. for more detail see code part

To create a horizontal bar chart, you need use barh() function (bar horizontal), in place of bar().The label that you gave to x-axis in an bar() will become y-axis label in barh() and vice versa.

For example:

import matplotlib.pyplot as pl

a = [10, 20, 30]

b = [29, 46, 65]

pl.barh (a, b, color = 'r', height=5)

pl.show( )



Histogram  A histogram is quite similar to vertical bar graph with no space in between vertical bars. When you have data which has data points fall between a particular range, you can use histogram to visualize this data. It is helpful to display statistical data or data inserted in measurable quantities. For ex. Marks, scores, units etc.

matplotlib.pyplot.hist(x, bins=value,cumulative=bool_val, histtype=type, align=alignment, orientation=orientation) where,

x: It is list to be plotted on histogram

bins: bins can be integer number computed with + 1 or generated by default.

cumulative: It is a boolean value i.e. either True or False. If provided True then bins are calculated where each bin gives the counts in that bin plus all bins for smaller values. The last bin gives total number of data points. The default values is false.

hisstype: It is an option parameter. It can be any one of these:

    1. bar: Bar type histogram, it arranges data side by side if given data is multiple. It is by default histtype.
    2. barstacked: When multiple data are stacked on top of each other
    3. step: Generates a lineplot that is by default unfilled
    4. stepfilled: Generates a lineplot that is by default filled

orientation: It can be used for bar – type histogram

import matplotlib.pyplot as m

x=[1,2,1,3,4,5,6,7,5,6,4,5,4,3,4,5,6,7,8,9,8,7,5,4,3,4,3,12,23,2,3,1,2,2,1,23,2,11,22,33,22,1,1]
m.hist(x,5,cumulative= True, edgecolor="blue",facecolor="r",orientation='vertical',histtype="barstacked"
#by default it is vertical
m.show()



Some important Data visualization code

import pandas as pd
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
y=[2000,2800,3000,2500,2300,2500,1000]
plt.plot(x,y,color="magenta",marker="*", label="No. of tickets")
plt.title("Day wise Tickets sold ")
plt.xlabel("Days")
plt.ylabel("No of tickets sold")
plt.legend()
plt.grid(True)
plt.savefig("Tickets.jpg")
plt.show()

OUT OUT of this code






Post a Comment

0 Comments