Data Visualization Using Matplotlib
Data Visualization Using Matplotlib
Matplotlib :
Python has different modules for visualizing data such as matplotlib, seaborn.
Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python. It presents data in 2D graphics. Seaborn is a visualization
library that is built on top of Matplotlib. It provides data visualizations that are
typically more aesthetic and statistically sophisticated. Matplotlib can be installed
using the following command:
Once the module installed, it must be imported into the program using the
following command
import matplotlib as mpl, where mpl is the alias name given to matplotlib library.
matplotlib.pyplot is a state-based interface to matplotlib. matplotlib.pyplot is a
collection of functionsthat make matplotlib work like MATLAB. Each pyplot
function makes some change to a figure: e.g., creates a figure, creates a plotting
area in a figure, plots some lines in a plotting area, decoratesthe plot with labels
etc. pyplot can be imported into the program using following command
1. Line plots
2. Area plots
3. Histograms
4. Bar charts
5. Pie charts
6. Box plots
7.scatter plots
LINE PLOTS:
Example:
PROGRAM:
x = [1, 2, 3, 4, 5, 6]
y = [1, 5, 3, 5, 7, 8]
plt.plot(x, y)
plt.show()
Output:
AREA PLOTS:
An Area Plot is also called as Area Chart which is used to display magnitude and
proportion of multiple variables.
Example:
Program:
days = [1,2,3,4,5]
sleeping =[7,8,6,11,7]
eating = [2,3,4,3,2]
working =[7,8,7,2,2]
playing = [8,5,7,8,13]
plt.plot([],[],color='m', label='Sleeping', linewidth=5)
plt.ylabel('y')
plt.title('Stack Plot')
plt.legend()
plt.show()
Output:
Histograms:
Example:
Program:
bins = [0,10,20,30,40,50,60,70,80,90,100]
plt.xlabel('age groups')
plt.ylabel('Number of people')
plt.title('Histogram')
plt.show()
Output:
Bar Charts:
A Bar chart or bar graph is a chart or graph that presents categorical data with
rectangular bars with heights or lengths proportional to the values that they
represent.
A bar plot is a way of representing data where the length of the bars represents
the magnitude/size of the feature/variable.
Example:
Program:
from matplotlib import pyplot as plt plt.bar([0.25,1.25,2.25,3.25,4.25],
[50,40,70,80,20],label="BMW",width=.5) plt.bar([.75,1.75,2.75,3.75,4.75],
[80,20,20,50,60],label="Audi", color='r',width=.5) plt.legend()
plt.xlabel('Days')
plt.ylabel('Distance (kms)')
plt.title('Information')
plt.show()
Output:
Pie Charts:
A Pie chart is a circular statistical chart, which is divided into sectors to illustrate
numerical proportion. Example:
Program:
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping =[7,8,6,11,7]
eating = [2,3,4,3,2]
working =[7,8,7,2,2]
playing = [8,5,7,8,13]
slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
cols = ['c','m','r','b']
plt.title('Pie Plot')
plt.show()
Output:
Box Plots:
A Box plot (or box-and-whisker plot) shows the distribution of quantitative data
in a way that facilitates comparisons between variables or across levels of a
categorical variable.
Box plot shows the quartiles of the dataset while the whiskers extend encompass
the rest of the distribution but leave out the points that are the outlier.
Program:
import matplotlib.pyplot as plt
x=[1,2,3,4,5,6,7]
y=[1,2,4,5,3,6,9]
z=[x,y]
plt.boxplot(z,labels=[“A”,”B”],showmeans=True)
plt.show()
Output:
Scatter Plots:
A Scatter chart, also called a scatter plot, is a chart that shows the relationship
between two variables.
Program:
x=[1,1.5,2,2.5,3,3.5,3.6]
y=[7.5,8,8.5,9,9.5,10,10.5]
x1=[8,8.5,9,9.5,10,10.5,11]
y1=[3,3.5,3.7,4,4.5,5,5.2]
plt.xlabel('saving*100')
plt.ylabel('income*1000')
plt.title('Scatter Plot')
plt.legend()
plt.show()
Output: