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

Data Visualization using Matplotlib in Python

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

Data Visualization using Matplotlib in Python

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Visualization using Matplotlib in Python


Data visualization is a crucial aspect of data analysis, enabling data scientists


and analysts to present complex data in a more understandable and insightful
manner. One of the most popular libraries for data visualization in Python is
Matplotlib. In this article, we will provide a comprehensive guide to using
Matplotlib for creating various types of plots and customizing them to fit
specific needs and how to visualize data with the help of the Matplotlib library
of Python.
Getting Started with Matplotlib
Matplotlib is a versatile and widely-used data visualization library in Python. It
allows users to create static, interactive, and animated visualizations.
The library is built on top of NumPy, making it efficient for handling large datasets.
This library is built on the top of NumPy arrays and consist of several plots like line
chart, bar chart, histogram, etc. It provides a lot of flexibility but at the cost of
writing more code.
Installing Matplotlib for Data Visualization
We will use the pip command to install this module. If you do not have pip installed
then refer to the article, Download and install pip Latest Version.
To install Matplotlib type the below command in the terminal.
pip install matplotlib

We can also install matplotlib in Jupyter Notebook and Google colab environment
by using the same command:

Installing Matplotlib for Data Visualization

If you are using Jupyter Notebook, you can install it within a notebook cell by
using:
!pip install matplotlib
Basic Plotting with Matplotlib : Step-by-Step Examples
Matplotlib supports a variety of plots including line charts, bar charts, histograms,
scatter plots, etc. We will discuss the most commonly used charts in this article
with the help of some good examples and will also see how to customize each
plot.
1. Line Chart
Line chart is one of the basic plots and can be created using the plot() function. It
is used to represent a relationship between two data X and Y on a different axis.
Syntax:
matplotlib.pyplot.plot(\*args, scalex=True, scaley=True, data=None, \*\*kwargs)
Example:
Python
1
import matplotlib.pyplot as plt

# initializing the data


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

# plotting the data


plt.plot(x, y)

# Adding title to the plot


plt.title("Line Chart")

# Adding label on the y-axis


plt.ylabel('Y-Axis')

# Adding label on the x-axis


plt.xlabel('X-Axis')

plt.show()
Output:

2. Bar Chart
A bar chart is a graph that represents the category of data with rectangular
bars with lengths and heights that is proportional to the values which they
represent. The bar plots can be plotted horizontally or vertically. A bar chart
describes the comparisons between the discrete categories. It can be created
using the bar() method.
In the below example, we will use the tips dataset. Tips database is the record of
the tip given by the customers in a restaurant for two and a half months in the early
1990s. It contains 6 columns as total_bill, tip, sex, smoker, day, time, size.
Example:
Python

import matplotlib.pyplot as plt


import pandas as pd

# Reading the tips.csv file

2
data = pd.read_csv('tips.csv')

# initializing the data


x = data['day']
y = data['total_bill']

# plotting the data


plt.bar(x, y)

# Adding title to the plot


plt.title("Tips Dataset")

# Adding label on the y-axis


plt.ylabel('Total Bill')

# Adding label on the x-axis


plt.xlabel('Day')

plt.show()
Output:

3. Histogram
A histogram is basically used to represent data provided in a form of some
groups. It is a type of bar plot where the X-axis represents the bin ranges while the
Y-axis gives information about frequency. The hist() function is used to compute
and create histogram of x.
Syntax:
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None,
cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’,
rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \
*\*kwargs)
Example:
Python

import matplotlib.pyplot as plt


import pandas as pd

# Reading the tips.csv file


data = pd.read_csv('tips.csv')

3
# initializing the data
x = data['total_bill']

# plotting the data


plt.hist(x)

# Adding title to the plot


plt.title("Tips Dataset")

# Adding label on the y-axis


plt.ylabel('Frequency')

# Adding label on the x-axis


plt.xlabel('Total Bill')

plt.show()
Output:

4. Scatter Plot
Scatter plots are used to observe relationships between variables.
The scatter() method in the matplotlib library is used to draw a scatter plot.
Syntax:
matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None,
marker=None, cmap=None, vmin=None, vmax=None, alpha=None,
linewidths=None, edgecolors=None
Example:
Python

import matplotlib.pyplot as plt


import pandas as pd

# Reading the tips.csv file


data = pd.read_csv('tips.csv')

# initializing the data


x = data['day']
y = data['total_bill']

# plotting the data


plt.scatter(x, y)

4
# Adding title to the plot
plt.title("Tips Dataset")

# Adding label on the y-axis


plt.ylabel('Total Bill')

# Adding label on the x-axis


plt.xlabel('Day')

plt.show()
Output:

5. Pie Chart
Pie chart is a circular chart used to display only one series of data. The area of
slices of the pie represents the percentage of the parts of the data. The slices of
pie are called wedges. It can be created using the pie() method.
Syntax:
matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None,
autopct=None, shadow=False)
Example:
Python

import matplotlib.pyplot as plt


import pandas as pd

# Reading the tips.csv file


data = pd.read_csv('tips.csv')

# initializing the data


cars = ['AUDI', 'BMW', 'FORD',
'TESLA', 'JAGUAR',]
data = [23, 10, 35, 15, 12]

# plotting the data


plt.pie(data, labels=cars)

# Adding title to the plot


plt.title("Car data")

plt.show()
Output:

5
6. Box Plot
A Box Plot, also known as a Whisker Plot, is a standardized way of displaying the
distribution of data based on a five-number summary: minimum, first quartile (Q1),
median (Q2), third quartile (Q3), and maximum. It can also show outliers.
Let’s see an example of how to create a Box Plot using Matplotlib in Python:
Python

import matplotlib.pyplot as plt


import numpy as np

np.random.seed(10)
data = [np.random.normal(0, std, 100) for std in range(1, 4)]

# Create a box plot


plt.boxplot(data, vert=True, patch_artist=True,
boxprops=dict(facecolor='skyblue'),
medianprops=dict(color='red'))

plt.xlabel('Data Set')
plt.ylabel('Values')
plt.title('Example of Box Plot')
plt.show()
Output:

Box Plot in Python using Matplotlib

6
Explanation:
 plt.boxplot(data): Creates the box plot. The vert=True argument makes the
plot vertical, and patch_artist=True fills the box with color.
 boxprops and medianprops: Customize the appearance of the boxes and median
lines, respectively.
The box shows the interquartile range (IQR), the line inside the box shows the
median, and the “whiskers” extend to the minimum and maximum values within 1.5
* IQR from the first and third quartiles. Any points outside this range are
considered outliers and are plotted as individual points.
7. Heatmap
A Heatmap is a data visualization technique that represents data in a matrix form,
where individual values are represented as colors. Heatmaps are particularly
useful for visualizing the magnitude of data across a two-dimensional surface and
identifying patterns, correlations, and concentrations.
Let’s see an example of how to create a Heatmap using Matplotlib in Python:
Python

import matplotlib.pyplot as plt


import numpy as np

np.random.seed(0)
data = np.random.rand(10, 10)

# Create a heatmap
plt.imshow(data, cmap='viridis', interpolation='nearest')
# Add a color bar to show the scale
plt.colorbar()

plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Example of Heatmap')

plt.show()
Output:

Heatmap with matplotlib

Explanation:
7
 plt.imshow(data, cmap='viridis') : Displays the data as an image (heatmap).
The cmap='viridis' argument specifies the color map used for the heatmap.
 interpolation='nearest' : Ensures that each data point is shown as a block of
color without smoothing.
The color bar on the side provides a scale to interpret the colors, with darker colors
representing lower values and lighter colors representing higher values. This type
of plot is often used in fields like data analysis, bioinformatics, and finance to
visualize data correlations and distributions across a matrix.
Advanced Plotting: Techniques for Visualizing Subplots
We have learned about the basic components of a graph that can be added so that
it can convey more information. One method can be by calling the plot function
again and again with a different set of values as shown in the above example. Now
let’s see how to plot multiple graphs using some functions and also how to plot
subplots.
Method 2: Using subplot() method
This method adds another plot at the specified grid position in the current figure.
Syntax:
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)
Example:
Python

import matplotlib.pyplot as plt

# initializing the data


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

# Creating figure object


plt.figure()

# adding first subplot


plt.subplot(121)
plt.plot(x, y)

# adding second subplot


plt.subplot(122)
plt.plot(y, x)
Output:

8
Error bars always run parallel to a quantity of scale axis so they can be displayed
either vertically or horizontally depending on whether the quantitative scale is on
the y-axis or x-axis if there are two quantity of scales and two pairs of arrow bars
can be used for both axes.

Let see an example of errorbar how it works.


Creating a Simple Graph.
 Python3

# importing matplotlib

import matplotlib.pyplot as plt

# making a simple plot

x =[1, 2, 3, 4, 5, 6, 7]

9
y =[1, 2, 1, 2, 1, 2, 1]

# plotting graph

plt.plot(x, y)

Output:

Example 1: Adding Some error in y value.


 Python3

# importing matplotlib
import matplotlib.pyplot as plt

# making a simple plot


x =[1, 2, 3, 4, 5, 6, 7]
y =[1, 2, 1, 2, 1, 2, 1]

# creating error
y_error = 0.2

# plotting graph
plt.plot(x, y)

plt.errorbar(x, y,
yerr = y_error,
fmt ='o')

Output:

10
Three-dimensional Plotting in Python using
Matplotlib
Last Updated : 22 Dec, 2023



3D plots are very important tools for visualizing data that have three
dimensions such as data that have two dependent and one independent
variable. By plotting data in 3d plots we can get a deeper understanding
of data that have three variables. We can use various matplotlib library
functions to plot 3D plots.
Example Of Three-dimensional Plotting using Matplotlib
We will first start with plotting the 3D axis using the Matplotlib library. For
plotting the 3D axis we just have to change the projection parameter
of plt.axes() from None to 3D.
 Python3

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = plt.axes(projection='3d')

Output:

11
3D Plots in MATLAB
Last Updated : 09 May, 2021



In MATLAB, we can plot different types of modules like 2d plotting and 3d


plotting. In this article, we will see what are the various types of 3D
plotting.
 Mesh Plot: A mesh plot is a 3d surface that creates different types of
meshes for different types of expression. To create mesh we have to
give the values x and y for z, (z= f(x, y)). For plotting the mesh plot it
has mesh() which will generate the 3d surface. It has solid edge color
but no face color.
Syntax:
 Surface plot: A surface plot is a 3d surface that creates different types of
surfaces for different expressions. To create a surface we have to give the
values x and y for z, (z= f(x, y)). For plotting the surface plot it has surf() which
will generate the 3d surface. It has solid edge color and solid face color
Syntax:
surf(Z)
Example:
 Matlab

% give the input for x and y


[x,y]= meshgrid(0:0.1:5);

% give the expression for


% x and y for the value of z.
z= sin(x).*cos(y);

% use surf() for the plotting


surf(z)

Output:

12
3D Heatmap in Python
Last Updated : 28 Jul, 2021



Heatmaps are a great way to visualize a dataset, methods for visualizing


the data are getting explored constantly and 3D heatmap is one of the
ways to plot data. Let’s learn how we can plot 3D data in python. We are
going to use matplotlib and mplot3d to plot the 3D Heatmap in Python.
We need to install the matplotlib explicitly by running the following
command in the console:
pip3 install matplotlib
Creating 3D heatmap
 In this code example, firstly we are using the
statement %matplotlib to display all matplotlib plots inline within the
jupyter notebook. Then we have imported all the necessary libraries
needed for running this code example – the mplot3d library is used to
draw 3d plots, pyplot is mainly for plotting the figure and its
configuration.
 After this, we have created a random dataset for our 3d heatmap using
NumPy randint function to create a random integer array.
 Using plt.figure, we have created a figure of size 10×10 width and
height respectively by default the matplotlib will produce 2D plots, so
to specify this as a 3d plot we use add_subplot function
with projection=’3d’ to create a 3d plot.
 We are mapping an array of integers as an RGBA color for our plot
using the set_array() function.
 After that, we are creating a scatter plot with our 3d dataset and by
setting marker value as s we are displaying each data point as
13
square-shaped. Their color will depend on the array that we have
created earlier called colo.
 At last, we have set the x, y, z labels and title using the set_label
function and displayed the plot using show() function.
Code:
 Python3
# 3D Heatmap in Python using matplotlib

# to make plot interactive


%matplotlib

# importing required libraries


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from pylab import *

# creating a dummy dataset


x = np.random.randint(low=100, high=500, size=(1000,))
y = np.random.randint(low=300, high=500, size=(1000,))
z = np.random.randint(low=200, high=500, size=(1000,))
colo = [x + y + z]

# creating figures
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')

# setting color bar


color_map = cm.ScalarMappable(cmap=cm.Greens_r)
color_map.set_array(colo)

# creating the heatmap


img = ax.scatter(x, y, z, marker='s',
s=200, color='green')
plt.colorbar(color_map)

# adding title and labels


ax.set_title("3D Heatmap")
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

# displaying plot
plt.show()

Output:

14
15

You might also like