Data Visualization
When data is shown in the form of pictures, it becomes easy
for the user to understand it. So representing the data in the
form of pictures or graph is called “data visualization”. It
represents (patterns, trends, correlations etc.) in data and
thereby helps decision makers to understand the meaning of
data for making decision in business.
Matplotlib is a python library which provides many
interfaces and function to present data in 2D graphics.
We can say, Matplotlib is a high quality plotting library
of Python.
Matplotlib library offers many different collections of
sub modules; Pyplot is one such sub module.
Pyplot is a collection of methods within Matplotlib
library which allows user to construct 2D plots easily.
importing Matplotlib-
To use Pyplot for data visualization, we have to first import
it in our python environment.
import matplotlib.pyplot
But this method will require to type every command as -
matplotlib.pyplot.Command
Another method is-
import matplotlib.pyplot as plt
(Now we can qualify command as plt.Command)
(plt is just identifier we can take any name)
Line Chart or Line Graph
Line graph is a simple graph that shows the result in the form
of lines. To create a line graph we need x and y coordinates.
For example-
plt.plot(x, y, ‘colorname’)
plot() function is used to draw line chart. In previous
examples we already observed this. Let us draw and use
various attributes available with plot().
CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD
KUMAR VERMA, PGT (CS) KV OEF KANPUR
Program:
Output:
Changing line color and line width and
line style :
Changing Marker Type, Size and Color
Marker- is a symbol (style) for representing data point.
Following is a list of valid marker style-
Marker Description
‘s’ Square Marker
‘o’ Circle Marker
‘d’ Diamond Marker
‘x’ Cross Marker
‘+’ Plus Marker
‘^’ Triangle down
‘v’ Triangle Up
Bar Graph
A bar graph is used to represents data in the form of vertical or horizontal bars.
It is useful to compare the quantities.
Changing Width, Color in Bar Chart :
Example 2-
Horizontal Bar Graph:
barh() is used to draw horizontal bar graph.
Output-
Multiple Bar Graph:
To draw multiple bar chart:
Decide the no. of X points, we can use arange() or
linspace() function to find no. of points based on the
length of values in sequence.
Decide the thickness of each bar and accordingly adjust
X point on X-axis
Give different color to different data ranges
The width remains the same for all ranges being plotted
Call plot() for each data range
Anatomy of chart:-
Setting Limits and Ticks
Pie Chart
A pie chart shows a circle that is divided into sectors and
each sector represents a proportion of the whole.
Sometimes we want to emphasize on one or
more slice and show them little pulled out. This
feature is called explode in pie chart.
If we want to explode or stand out 2nd and 3rd slice
out of 5 slices to 0.2 and 0.3 unit respectively ,
explode will be [0,0.2,0.3,0,0]. The value of
explode vary from 0.1 to 1 to show that how
much a slice will come out of pie chart.
autopct : allows to view percentage of share
in a pie chart-
The option autopct=’%.1f %%’ indicates how to display the
percentages on the slices. Here %.1 shows that the
percentage value should be displayed with 1 digit after
decimal point. The next two % symbols indicates that only
one symbol is to be displayed.
Shadow option-
Shadow= True indicates that the pie chart should be
displayed with a shadow. This will improve the look of the
chart.
Setting ticks of Bar Graph:-
Histogram
Histogram shows distribution of values. Histogram is similar to bar
graph but it is useful to show values grouped in bins or intervals.
For example- we can collect the age of each employee in an office and
show it in the form of a histogram to know how many employees are
there in the range 0-10 years, 10-20 years and so on. For this we can
create histogram like this-
Output-
rwidth=0.6 means
that the
bars width is 60%.
There will be a
gap of 40% space
before and after
the bar.
Example 2-
Output-
Note- edgecolor is used to define the color of edge around
bar.