Data Visulation
Data Visulation
Importing PyPlot
To import Pyplot following syntax is
import matplotlib.pyplot
or
import matplotlib.pyplot as plt
After importing matplotlib in the form of plt we can use plt for accessing any function of
matplotlib
Line Plot:
A line plot/chart is a graph that shows the frequency of data occurring along a number line.
The line plot is represented by a series of data points called markers connected with a
straight line. Generally, line plots are used to display trends over time. A line plot or line
graph can be created using the plot () function available in pyplot library.
We can, not only just plot a line but we can explicitly define the grid, the x and y axis scale
and labels, title and display options etc.
• We can create line graph with x coordinate only or with x and y coordinates.
• Function to draw line chart – plot()
• Default colour of line- blue
The default width for each bar is .0.8 units, which can be changed.
• Syntax: plt.plot(x,y)
Line Plot customization
• Custom line color
plt.plot(x,y,'red')
Change the value in color argument like ‘b’ for blue,’r’,’c’,…..
• Label-
plt.xlabel(‘TIme') – to set the x axis label
plt.ylabel(‘Temp') – to set the y axis label
Changing Marker Type, Size and Color
plt.plot(x,y,'blue',marker='*',markersize=10,markeredgecolor='magenta')
57
HISTOGRAM
Creating a Histogram :
It is a type of bar plot where X-axis represents the bin ranges while Y-axis gives information
about frequency.
To create a histogram the first step is to create bin of the ranges, then distribute the whole
range of the values into a series of intervals, and count the values which fall into each of
the intervals.
Bins are clearly identified as consecutive, non-overlapping intervals of variables.
Syntax:
plt.hist(x,other parameters)
Optional Parameters
x array or sequence of array
PROGRAM :
• Legend - A legend is an area describing the elements of the graph. In the matplotlib library
there is a function named legend() which is used to place a legend on the axes .
When we plot multiple ranges in a single plot ,it becomes necessary that legends are specified.It
is a color or mark linked to a specific data range plotted .
i)In the plotting function like bar() or plot() , give a specific label to the data range using label
ii)Add legend to the plot using legend ( ) as per the sytax given below .
position number can be u1,2,3,4 specifying the position strings upper right/'upper left/'lower
left/lower right respectively .
Default position is upper right or 1
To save any plot savefig() method is used. Plots can be saved in various formats like
pdf,png,eps etc .
plt.savefig('line_plot.pdf') // save plot in the current directory
plt.savefig('d:\\plot\\line_plot.pdf') // save plot in the given path