Chapter 3-Plotting With PyPlot
Chapter 3-Plotting With PyPlot
●
Data visualization basically refers to the graphical or visual
representation of information and data using visual elements
like charts,graphs , and maps etc.
●
Data visualization is immensely useful in decision-making.
●
Data visualization unveils(uncover) like
patterns ,trends,outliers, correlation etc in the data, and thereby
helps decision -makers understand the meaning of data to drive
business decisions.
3.2 Using Pyplot of Matplotlib Library
●
For data visualization in Python, the Matplotlib library's Pyplot
interface is used.
●
Matplotlib is a high quality plotting library of Python.It
provides a quick way to visualize data from Python and
publication-quality figures in many formats.
●
The Matplotlib is a Python library that provides many
interfaces and functionality for 2D- graphics.
●
Pyplot is one such interface that has a collection of methods ,
which allows user to construct 2D plots easily
3.2.1 Installing and Importing matplotlib
●
If you have installed Python using Anaconda, then matplotlib
library is already installed.
●
If you have installed Python using standard official distribution ,
you may need to install matplotlib by following steps
1. Download required package from the link
https://pypi.org/project/matplotlib/#files
2. Install it by following command from the command prompt:
python -m pip install -U pip
python -m pip install -U matplotli
You can now Import Pyplot interface by using following command:
import matplotlib.pyplot
Or
import matplotlib.pyplot as pl
3.2.2 Working with PyPlot Methods
●
The PyPlot interface provides many methods for 2D plotting of
data.
●
Have a look at below example where we are plotting a simple
chart using an ndarray.
3.2.2A Basics of Simple Plotting
Color code
e.g plt.plot([1,2,3,4] ,[40,80,100,50],'b')
●
To Change Line Width using linewidth = <width> argument
plt.plot(x,a,linewidth = 2)
Change the line style using linestyle or ls =['solid' | 'dashed'
|'dashdot' | 'dotted']
●
plt.plot(x,a,linewidth = 2)
●
plt.plot(x,b,linewidth = 4, linestyle ='dashed')
Changing Marker Type, Size and Color
●
marker = <valid marker type>, markersize = <in points>,markeredgecolor = <valid color>
3.3.4 Creating Scatter Charts
“ The scatter chart is a graph of plotted points on two axes that
show the relationship between two sets of data.”
NOTE
The primary difference of scatter( ) from plot() is that it can be
used to create scatter plots where the properties of each individual
point (size,face color,edge color,etc) can be individually controlled
or mapped to data.
3.4 Creating Bar Charts and Pie Charts
●
A Bar Graph or a Bar Chart is a graphical display of data using bars of
different heights.
●
Chart can be drawn vertically or horizontally using retangles or bars of
different hights/widths.
●
PyPlot offers bar( ) function to create a bar chart where you can specify
the sequence for x-axis and corresponding sequence to be plotted on y-
axis.
●
e.g. A,b,c = [1,2,3,4] , [2,4,6,8] , [1,4,9,16]
●
Consider another example
3.4.1 Changing Widths of the Bars in a Bar Chart
●
By default, bar chart draws bars with equal widths and having a
default width of 0.8 units on a bar chart.
●
But we can specify a different width for all the bars of a bar
chart.
plt.bar( <x-sequence>, <y-sequence>, width = <float value> )
●
We can also specify different widths for different bars of a bar
chart.
plt.bar( <x-sequence>, <y-sequence>, width = <width values
sequence> )
N O T E : The width values sequence in a bar( ) must have widths for all the
bars, i.e. Its length must match the length of data sequence being plotted, otherwise
Python will report an error
3.4.2 Changing Colors of the Bars in a Bar Chart
●
By default . A bar chart draws bars with same default color.But
you can always change the color of the bars.
●
To specify a different color for all the bars
plt.bar( <x – sequence>, <y – sequence>, color = <color code/name>)
●
To specify a different color for different bars of a bar chart
plt.bar( <x – sequence>, <y – sequence>, color = <color code/name
sequence>)
3.4.3 Creating Multiple Bars Chart
Often in real life, you may need to plot multiple data ranges on
the same bar chart creating multiple bars.PtPlot does not
provide a specific function for this, but you can always create
one exploting the width and color argument of bar( ) that you
learnt above.
1. Decide number of X points.
2. Decide thickness of each bar and accordingly adjust X points
on X- axis
3. Give different color to different data range.
4. The width argument remains the same for all ranges being
plotted.
5. Plot using bar( ) for each range seperately.
3.4.4 Creating a Horizontal Bar Chart
●
To create a horizontal bar chart, you need to use barh( )
function (bar horizontal), in place of bar( ). Also , you need to
give a and y axis labels carefully – the label that you gave to x
axis in bar( ), will become y-axis' label in barh( ) and vice-
versa.
3.4.5 Creating Pie Chart
“ The Pie chart is a type of graph in which a circle is divided
into sectors that each represent a proportion of the whole ”
●
Typically, a Pie Chart is used to show parts to the whole and often
a % share.
●
The PyPlot interface offers pie( ) function for creating a pie chart.
i) The pie( ) function, plots a single data range only.
ii) The default shape of a pie chart is oval but you always change
to circle by using axis() of pyplot, sending “equal” as argument to
it.
e.g contri = [17,8.8, 12.75,14]
plt.pie(contri)
Labels of Slices of Pie
●
We need to create a sequence containing the labels and then
specify this sequence as value for labels argument of pie( )
function.The first label is given to first value, second label to
second value and so on.
Contri = [17,8.8,12.75,14]
houses = ['Vidya', 'Kshama' , 'Namrta', 'Karuna']
plt.pie(contri, labels = houses)
●
Adding Formatted Slice Percentages to Pie
To view percentage of share ina pie chart, you need to add an
argument autopct with a format string, such as “% 1.1F%%”.
pl.hist( [ x , y ] )
6. Plot a stacked bar type histogram from both ndarray x and y
●
(a) regular histogram ●
(b) cumulative histogram
pl.hist( [ x, y] , histtype = 'barstacked' pl.hist( [ x, y] , histtype =
'barstacked',cumulative = True )
7. Plot a horizontal histogram from from ndarray y with 50 bins
●
To plot DataFrame's data, just pass its column name to the
Pyplot's graph functions ( plot( ) , bar( ), barh( ), scatter( ),
boxplot( ), hist( ) ).
●
It will treat the passed column's data as a Series and plot it .
Example:
●
We can also plot a bar chart using this Dataframe's data as :
plt.bar(df2.index , df2.Projects)
●
The plot( ) can take a DataFrame's name and will plot all
columns
import pandas as pd
import matplotlib.pyplot as plt
: # df2 created or loaded
plt.plot(df2)
3.9.2 Plotting a DataFrame's Data using DataFrame's plot()
Pandas provides a function plot( ) which you can use with
DataFrame's as:
<DF>.plot( )
The DataFrame's plot( ) is a versatile function, which can plot
all types of chart by just specifying kind argument.
Advantages
●
It plots only the numeric columns unlike plot( ) of PyPlot
when used with a DataFrame.
●
It automatically adds legends for the plotted data.