0% found this document useful (0 votes)
0 views

Unit 4 Data Visualization using Matplotlib - Copy

This document provides an overview of data visualization using Matplotlib, a powerful Python library for creating various types of plots and charts. It covers installation, basic plotting techniques, customization options, and advanced features such as 3D plotting and logarithmic axes. The document also includes learning objectives, practical labs, quizzes, and references for further exploration of the topic.

Uploaded by

rohini.d.patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Unit 4 Data Visualization using Matplotlib - Copy

This document provides an overview of data visualization using Matplotlib, a powerful Python library for creating various types of plots and charts. It covers installation, basic plotting techniques, customization options, and advanced features such as 3D plotting and logarithmic axes. The document also includes learning objectives, practical labs, quizzes, and references for further exploration of the topic.

Uploaded by

rohini.d.patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Data Visualization using Matplotlib

Unit 4
Data Visualization
using Matplotlib
Data Visualization using Matplotlib

Disclaimer
The content is curated from online/offline resources and used for educational purpose only
Data Visualization using Matplotlib

Learning Objectives
• Introduction to Matplotlib
• Matplotlib Installation
• Data Visualization
• Plotting and Visualization
• Figures and Subplot
• Colours, marker, and Line Style
• Text Annotation
• Three-Dimensional Plotting
• Logarithmic Axes in Matplotlib
Data Visualization using Matplotlib

Introduction to Matplotlib
• Matplotlib is a free and open-source drawing library that can work with a variety of drawing formats.
• You can generate plots, histograms, bar charts, and other types of charts with just a few lines of code.
• Matplotlib is an excellent Python visualization library for two-dimensional array plots.
Data Visualization using Matplotlib

Installing Matplotlib
Install Matplotlib in the command prompt terminal by running the following command:

Using pip • pip install matplotlib

Using conda • Conda install matplotlib


Data Visualization using Matplotlib

What is Data Visualization?


• The graphical representation of information and
data is known as data visualisation.
• Data visualisation tools, which use visual elements
such as charts, graphs, and maps, make it easy to
see and understand trends, outliers, and patterns
in data.
Data Visualization using Matplotlib

Families of Visualizations​

Chart​ Geo-visualization​ Tables


Data Visualization using Matplotlib

Plotting and Visualization


• The graphical representation of information and data is known as data visualisation.
• Data visualisation tools, which use visual elements such as charts, graphs, and maps, make it
easy to see and understand trends, outliers, and patterns in data.

To get a little overview here are a few popular plotting libraries:

• Matplotlib: low level, provides lots of freedom


• Pandas Visualization: easy to use interface, built on
Matplotlib
• Seaborn: high-level interface, great default styles
• ggplot: based on R’s ggplot2, uses Grammar of Graphics
• Plotly: can create interactive plotss
Data Visualization using Matplotlib

Lab 1 Installation of Matplotlib and Plotting first


Visualization using Matplotlib
Data Visualization using Matplotlib

Graph Plot using Matplotlib​

Import matplotlib.pyplot as plt​


// Import matplotlib ​
X = [1,2,3]​
//x-axis coordinates​
Y= [3,4,5]​
//y-axis coordinates​

plt.plot(x,y)​
// call plot function to draw plot ​
plt.show()​
//call show function to show plot
on output​
Data Visualization using Matplotlib

Figures and Subplots


Figures in Matplotlib
• The overall window or page on which everything is drawn is referred to as the Figure.
• The declaring –
• Syntax : fig = plt.figure()
Data Visualization using Matplotlib

Figures and Subplots


Subplots in Matplotlib
• You can plot two or more plots together in one figure by using the Matplotlib subplot() function.
• The subplot() function takes three arguments that describes the layout of the figure.
• The layout is organized in rows and columns, which are represented by the first and second argument.

plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.
plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
Data Visualization using Matplotlib

Figures and Subplots


Subplots in Matplotlib
Data Visualization using Matplotlib

Lab 2 Playing with Plots using Matplotlib, Graphs most


preferable by Data Analyst.
Data Visualization using Matplotlib

Colors, Markers and Line Styles


Marker keyword used to emphasize each point with a specifies marker​

import matplotlib.pyplot as plt​


import numpy as np​

ypoints = np.array([3, 8, 1, 10])​

plt.plot(ypoints, marker = 'o')​
plt.show()​
Data Visualization using Matplotlib

Colors, Markers and Line Styles


You can use the keyword argument color or the shorter c to set the color of the line.​

import matplotlib.pyplot as plt​


import numpy as np​

ypoints = np.array([3, 8, 1, 10])​

plt.plot(ypoints, color = 'r')​
plt.show()​
Data Visualization using Matplotlib

Colors, Markers and Line Styles


Linestyle is keyword used to change the style of the plotted line:​

import matplotlib.pyplot as plt​


import numpy as np​

ypoints = np.array([3, 8, 1, 10])​

plt.plot(ypoints, linestyle = 'dotted')​
plt.show()
Data Visualization using Matplotlib

​Line Plot​
• Python line plots can be made using the pyplot library from Matplotlib.
• Load Matplotlib first in order to create a line plot. Importing the pyplot library from Matplotlib as plt is
standard procedure.
• The plt alias will be recognizable to other Python programmers.
Data Visualization using Matplotlib

Bar Charts
• To create a bar plot with Matplotlib, first import the pyplot library.
• The alias plt is frequently used to replace matplotlib.pyplot.
Data Visualization using Matplotlib

Scatter Plots
• To create scatter plots of (x,y) point pairs, use the scatter() method of the Matplotlib library.
• For scatter(), two lists or arrays must be provided as positional arguments.
• The x-value of each scatter plot point is specified by the first positional argument.
• The y-value of each scatter plot point is specified by the second positional argument.
Data Visualization using Matplotlib

Legend in the Graph

Legends in the graph are very similar to that we have our name on the mark sheet so whatever is written
there in the marksheet is basically mine similar to that whenever we define any plot we have to put some
legends for specifying data on an axis
Data Visualization using Matplotlib

Label
Data Visualization using Matplotlib

Ticks
• The values used to display particular points on the coordinate axis are called ticks.
• It might be a string or a number. The axes automatically adjust and take the default ticks whenever we
plot a graph.
• The default ticks provided by Matplotlib are generally adequate in common scenarios, but they are not
ideal for every plot.
Data Visualization using Matplotlib

Histograms and Binning


• An accurate graphic representation of the distribution of numerical data is a histogram.
• It is a prediction of a continuous variable's (quantitative variable) probability distribution.
• It is a kind of bar graph.
Data Visualization using Matplotlib

Binning
• Binning is the process of dividing the data into these bins. When you create a histogram, you specify the
number of bins you want to use, or sometimes the bin edges are automatically determined based on the
data range and distribution
Data Visualization using Matplotlib

Text and Annotations


• Text and annotations are essential elements in data visualization that add context, explanations,
labels, and additional information to plots, charts, graphs, and other visual representations of data.

• They help convey insights, highlight key points, and make visualizations more understandable and
interpretable for viewers.
Data Visualization using Matplotlib

Pie Chart
• A pie chart is a circular data visualization that represents data as slices of a pie. Each slice
corresponds to a specific category or data group, and the size of the slice is proportional to the value
it represents.
Data Visualization using Matplotlib

Lab 3 Advance Data Plotting Techniques using Matplotlib


Data Visualization using Matplotlib

Three-Dimensional Plotting in Matplotlib


• the
• from mpl_toolkits import mplot3d
Data Visualization using Matplotlib

Plotting 3-D Lines and Points


• ​The simplest three-dimensional graph is one that has only lines and points.
• The function,
Data Visualization using Matplotlib

Logarithmic Axes in Matplotlib


• ​All plots created with Matplotlib's yscale() and xscale() methods have linear axes by default.
• The y-axis or x-axis scale can be changed to logarithmic using the pyplot library, respectively.
• To convert axes to a logarithmic scale, we pass the "log" keyword or the matplotlib.scale method's
single parameter, which specifies the scale's type of conversion.

Without Logarithmic Axes.

import matplotlib.pyplot as plt

# exponential function y = 10^x


data = [10**i for i in range(5)]

plt.plot(data)
Data Visualization using Matplotlib

Logarithmic Axes in Matplotlib


y-axis Logarithmic Scale.

import matplotlib.pyplot as plt

# exponential function y = 10^x


data = [10**i for i in range(5)]

# convert y-axis to Logarithmic scale


plt.yscale("log")

plt.plot(data)
Data Visualization using Matplotlib

Logarithmic Axes in Matplotlib

x-axis Logarithmic Scale.

import matplotlib.pyplot as plt

# exponential function x = 10^y


datax = [ 10**i for i in range(5)]
datay = [ i for i in range(5)]

#convert x-axis to Logarithmic scale


plt.xscale("log")

plt.plot(datax,datay)
Data Visualization using Matplotlib

Summary
In this session we have learned -
• Data visualization with Matplotlib, a versatile and powerful Python library.
• Matplotlib showcases its ability to create stunning and customizable charts, plots, and graphs.
• From basic line plots to complex 3D visualizations, Matplotlib empowers users to convey insights from
data effectively.
• Discover its intuitive interface, rich customization options, and seamless integration with other data
manipulation libraries.
• We need to Unleash the potential of Matplotlib to transform raw data into compelling visual narratives
that aid analysis and decision-making.
Data Visualization using Matplotlib

Quiz​
1. What is the purpose of Matplotlib’s pyplot module?

a) To create data visualizations


b) To manage data storage
c) To manipulate data frames
d) To install third-party packages

Answer: a) To create data visualizations


Data Visualization using Matplotlib

Quiz​
2. Which of the following is not a type of Matplotlib plot?

a) Line plot
b) Scatter plot
c) Pie chart
d) Bar chart

Answer: c) Pie chart


Data Visualization using Matplotlib

Quiz​
3. How can you add a title to a Matplotlib plot?

a) By using the title() function


b) By using the label() function
c) By using the text() function
d) By using the legend() function

Answer: a) By using the title() function


Data Visualization using Matplotlib

Quiz​
4. What is the default color for Matplotlib plots?

a) Red
b) Blue
c) Green
d) Black

Answer: b) Blue
Data Visualization using Matplotlib

Quiz​
5. What is the purpose of the legend() function in Matplotlib?

a) To label the x and y axes of a plot


b) To add a title to a plot
c) To add annotations to a plot
d) To label different lines or markers on a plot

Answer: d) To label different lines or markers on a plot


Data Visualization using Matplotlib

HANDS ON...!
Data Visualization using Matplotlib

References
• https://www.tableau.com/learn/articles/data-visualization/glossary​
• https://www.w3schools.com/python/matplotlib_markers.asp​
• https://www.geeksforgeeks.org/matplotlib-pyplot-legend-in-python/​
• https://jakevdp.github.io/PythonDataScienceHandbook/04.09-text-and-annotation.html​
• https://www.geeksforgeeks.org/three-dimensional-plotting-in-python-using-matplotlib/​
• https://www.tutorialspoint.com/numpy/numpy_matplotlib.htm
Data Visualization using Matplotlib

Thank you...!

You might also like