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

Matplotlib in Python

Matplotlib is a widely used plotting library in Python that facilitates data visualization through various graphical tools, enabling users to create a range of plots such as line graphs, bar charts, and pie charts. It is open-source, user-friendly, and integrates well with other libraries like NumPy and Pandas, making it suitable for both beginners and experienced users. The library supports exporting visualizations in multiple formats and is applicable in various environments, including scripts and Jupyter Notebooks.

Uploaded by

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

Matplotlib in Python

Matplotlib is a widely used plotting library in Python that facilitates data visualization through various graphical tools, enabling users to create a range of plots such as line graphs, bar charts, and pie charts. It is open-source, user-friendly, and integrates well with other libraries like NumPy and Pandas, making it suitable for both beginners and experienced users. The library supports exporting visualizations in multiple formats and is applicable in various environments, including scripts and Jupyter Notebooks.

Uploaded by

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

What is Matplotlib in Python?

Matplotlib is one such solution for


Python users that need to visualize their
data in order to make essential
statistical conclusions. It is a complete
plotting package that is useful for
Python and NumPy users. This article
will assist you in comprehending the
matplotlib library, which is frequently
used in the industry. Matplotlib
includes a wide range of graphical tools
and is simple to use.
Introduction
Data visualization is one of the
important skills that data scientists are
expected to have. Visualization
techniques can be used to understand
and address the majority of business
challenges. Exploratory Data Analysis
(EDA) and Graphical Plots are the two
main components of visualization.
Effective visualization helps users in
understanding data trends and solving
business problems more efficiently.
Another advantage of visualization is
that it reduces complex data to a more
readable manner.
A visual is significantly simpler to
understand than text for most people.
Visualization is the most effective
communication tool for analyzing and
interpreting data. It allows customers to
quickly interpret vast amounts of data.
Trends, correlations, patterns,
distributions, and so on can all be better
understood through data visualization.
For data visualization, there are
numerous tools and technologies
available in the market, with Python
being the most popular. Python has
many libraries for data visualization; a
few of the most prominent graphic
libraries are:
 Matplotlib
 Seaborn
 Pandas visualization
 Plotly
What is Matplotlib in Python?
Matplotlib is the basic plotting library
of the Python programming language.
Among Python visualization packages,
it is the most widely used.
Matplotlib is exceptionally fast at a
variety of operations. It can export
visualizations to all popular formats,
including PDF, SVG, JPG, PNG,
BMP, and GIF.
It can create line graphs, scatter plots,
histograms, bar charts, error charts,
pie charts, box plots, and many other
visualization styles. 3D charting is also
possible with Matplotlib.
Matplotlib serves as the foundation for
several Python libraries. Matplotlib is
used to build pandas and Seaborn, for
example. They make it possible to
access Matplotlib's methods with less
code.
John Hunter founded the Matplotlib
project in 2002. Matplotlib was created
during post-doctoral study in
Neurobiology to
show Electrocorticography
(ECoG) data from epileptic patients.
Matplotlib, an open-source plotting
toolkit for the Python programming
language, has become the most
extensively used plotting library. It was
used to visualize data during the 2008
landing of the Phoenix spacecraft.
Why is Matplotlib so Popular?
Matplotlib's popularity can be
explained as follows:
 For beginners, it's simple and
straightforward.
 It's open-source and free.
 Matplotlib is a highly customized
and robust
 Matplotlib is good at working with
data frames and arrays. Figures and
axes are treated as objects. It has a
number of stateful plotting APIs. As
a result, methods like plot() can be
used without any parameters.
 Those who have used Matlab or
other graph plotting tools before will
find Matplotlib to be easy to use.
 Matplotlib can be used in a variety
of contexts, such as Python scripts,
the Python and iPython shells,
and Jupyter Notebooks.
 Matplotlib is a 2-D plotting library.
However, there are several
extensions that can be used to
produce complex visualizations such
as 3-D graphs, etc.
 It offers high-quality photos and
plots in a variety of formats,
including png, pdf, and pgf.
 Controls numerous aspects of a
figure, including DPI, figure color,
and figure size.
Examples of Matplotlib in Python
Example 1: Create several line charts
on a shared plot by plotting two data
ranges on the same chart. The data
range(s) to be plotted are as follows:
line 1 points x1 = [40,50,60]
y1 = [10,30,60]
line 2 points x2 = [40,50,60]
y2 = [60,30,10]
The legend can be seen in the upper
left corner. The X axis is labeled "X –
Axis," and the Y axis is labeled "Y –
Axis." The lines should be of varying
width and color.
import matplotlib.pyplot as plt
# points at line 1
x1 = [40,50,60]
y1 = [10,30,60]
# points at line 2
x2 = [40,50,60]
y2 = [60,30,10]
plt.xlabel('x - axis')
plt.ylabel('y - axis')
# Display the figure.
plt.plot(x1,y1, color='g', linewidth = 2,
label = 'line_1')
plt.plot(x2,y2, color='y', linewidth = 4,
label = 'line_2')
plt.legend()
plt.show()

Output:
Example 2:
Make a bar chart using the following
data from three runners over four
marathons:
Runners = [[3,15,35,30],[5,28,59,37],
[7,24,49,29]]
import numpy as np
import matplotlib.pyplot as plt
Run = [[3,15,35,30],[5,28,59,37],
[7,24,49,29]]
X=np.arange(4)
plt.bar(X+0.00,Run[0],color='c',width=0.20)
plt.bar(X+0.25,Run[1],color='m',width=0.20)
plt.bar(X+0.5,Run[2],color='k',width=0.20)
plt.show()

Example 3: Create a pie chart for a


student's grades, using the following
data:
* _Physics - 99 * _English - 98 *
_Math - 93 * _Chemistry - 97 *
_Biology - 95
import matplotlib.pyplot as plt
_Marks=[99,98,93,97,95]
_Subjects=["_Physics","_English","_M
ath","_Chemistry","_Biology"]
plt.axis("equal")
plt.pie(_Marks,labels=_Subjects,explod
e=[0,0,0,0,0.2],autopct="%1.2f%%")
plt.show()
Output:

Features of Matplotlib
 It is a data visualization package for
the Python programming language.
 It is the most basic and widely used
method for plotting data in Python.
 It includes tools for creating
publication-standard plots and
figures in a number of export
formats and
environments (pycharm, jupyter
notebook) across platforms.
 It includes a procedural interface
called Pylab, which is supposed to
behave similarly to MATLAB, a
programming language popular
among scientists and
researchers. MATLAB is a
commercial application that is not
open source.
 It's comparable to MATLAB
plotting in that it gives users
complete control over fonts, lines,
colours, styles, and axes attributes.
 Matplotlib with NumPy might be
considered the open source version
of MATLAB.
 It is a great approach to create high-
quality static graphics for
publications and professional
presentations.
 It also works with a variety of
different third-party libraries and
packages, allowing matplotlib to
expand its capabilities.
 It is clear that matplotlib with its
various compatible third-party
libraries provide users with powerful
tools to visualize a variety of data.
Applications of Matplotlib
 Matplotlib creates significant figures
in a number of physical and
graphical formats across various
platforms.
 Matplotlib is a Python library that
can be used in scripts.
 Matplotlib is a Python/IPython
library that can be used in shells.
 Web application servers can utilise
Matplotlib.
 Matplotlib is a graphical user
interface toolkit that may be used in
a variety of graphical user interface
toolkits.

Basic plots in Matplotlib :


Matplotlib comes with a wide variety of
plots. Plots helps to understand trends,
patterns, and to make correlations.
They’re typically instruments for
reasoning about quantitative
information. Some of the sample plots
are covered here.
Line plot :
# importing matplotlib module
from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot
plt.plot(x,y)

# function to show the plot


plt.show()
Output :
Bar plot :
# importing matplotlib module
from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot the bar


plt.bar(x,y)

# function to show the plot


plt.show()
Output :

Histogram :
# importing matplotlib module
from matplotlib import pyplot as plt

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot histogram


plt.hist(y)
# Function to show the plot
plt.show()
Output :

Scatter Plot :
# importing matplotlib module
from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot scatter
plt.scatter(x, y)

# function to show the plot


plt.show()
Output :

Matplotlib Line Plot


In this example, pyplot is imported as plt, and then used to plot
three numbers in a straight line:

import matplotlib.pyplot as plt

# Plot some numbers:

plt.plot([1, 2, 3])
plt.title(”Line Plot”)

# Display the plot:


plt.show()

Figure 1. Line plot generated by Matplotlib:

Matplotlib Pie Plot


In this example, pyplot is imported as plt, and then used to
create a chart with four sections that have different labels,
sizes and colors:

import matplotlib.pyplot as plt

# Data labels, sizes, and colors are defined:

labels = 'Broccoli', 'Chocolate Cake', 'Blueberries', 'Raspberries'

sizes = [30, 330, 245, 210]

colors = ['green', 'brown', 'blue', 'red']


# Data is plotted:
plt.pie(sizes, labels=labels, colors=colors)

plt.axis('equal')
plt.title(“Pie Plot”)
plt.show()

Figure 2. Pie plot generated by Matplotlib:

Matplotlib Bar Plot


In this example, pyplot is imported as plt, and then used to
plot three vertical bar graphs:
import matplotlib.pyplot as plt

import numpy as np

# Create a Line2D instance with x and y data in sequences xdata, ydata:

# x data:
xdata=['A','B','C']

# y data:
ydata=[1,3,5]

plt.bar(range(len(xdata)),ydata)
plt.title(“Bar Plot”)
plt.show()
Figure 3. Bar plot generated by Matplotlib:

Matplotlib: Plot a Numpy Array


In this example, pyplot is imported as plt, and then used to
plot a range of numbers stored in a numpy array:

import numpy as np

from matplotlib import pyplot as plt

# Create an ndarray on x axis using the numpy range() function:

x = np.arange(3,21)

# Store equation values on y axis:

y=2*x+8

plt.title("NumPy Array Plot")

# Plot values using x,y coordinates:


plt.plot(x,y)
plt.show()

You might also like