Matplotlib in Python
Matplotlib in Python
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()
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.
# 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)
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
Histogram :
# importing matplotlib module
from matplotlib import pyplot as plt
# Y-axis values
y = [10, 5, 8, 4, 2]
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)
plt.plot([1, 2, 3])
plt.title(”Line Plot”)
plt.axis('equal')
plt.title(“Pie Plot”)
plt.show()
import numpy as np
# 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:
import numpy as np
x = np.arange(3,21)
y=2*x+8