Matplotlib
Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
Matplotlib was created by John D. Hunter.
Matplotlib is open source and we can use it freely.
Code Text
Installation of Matplotlib
First, we should ensure that, Python and PIP are installed in the system, then can install matplotlib using the following pip command:
pip install matplotlib
Import Matplotlib:
After successful installation of matplotlib, we can import it using the following import module statement:
import matplotlib
import matplotlib
Checking Matplotlib Version
The version string is stored under __version__ attribute.
import matplotlib
print(matplotlib.__version__)
3.7.2
Matplotlib Pyplot
Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the plt alias:
import [Link] as plt
# Draw a line in a diagram from position (0,0) to position (6,250):
# From these points, can draw straight line
xpoints = [Link]([0, 6])
ypoints = [Link]([0, 250])
[Link](xpoints, ypoints)
[Link]()
# plotting points, which are not in a straight line
x_pts = [Link]([3, 5, 8, 10, 15, 24])
y_pts = [Link]([10, 25, 23, 12, 58, 56])
[Link](x_pts, y_pts)
[Link]()
Plotting x and y points:
The plot() function is used to draw points (markers) in a diagram.
By default, the plot() function draws a line from point to point.
The function takes parameters for specifying points in the diagram.
Parameter 1 is an array containing the points on the x-axis.
Parameter 2 is an array containing the points on the y-axis.
# Draw straight line between 2 points (1,3) and (8,10)
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])
[Link](xpoints, ypoints)
[Link]()
Plotting Without Line:
To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.
# Just mark 2 points at the places: (1,3) and (8,10)
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])
[Link](xpoints, ypoints, 'o')
[Link]()
Matplotlib for beginners
Matplotlib is a library for making 2D plots in Python. It is
Z = [Link](0, 1, (8, 8))
Organize
designed with the philosophy that you should be able to
create simple plots with just a few commands: You can plot several data on the same figure, but you can
[Link](Z) also split a figure in several subplots (named Axes):
1 Initialize
Z = [Link](0, 1, 4) X = [Link](0, 10, 100)
import numpy as np Y1, Y2 = [Link](X), [Link](X)
import [Link] as plt [Link](Z) [Link](X, Y1, X, Y2)
Z = [Link](0, 1, 100) fig, (ax1, ax2) = [Link](2, 1)
2 Prepare
[Link](X, Y1, color=”C1”)
X = [Link](0, 10*[Link], 1000) [Link](Z) [Link](X, Y2, color=”C0”)
Y = [Link](X)
X = [Link](5) fig, (ax1, ax2) = [Link](1, 2)
3 Render Y = [Link](0, 1, 5) [Link](Y1, X, color=”C1”)
[Link](X, Y, Y∕4) [Link](Y2, X, color=”C0”)
fig, ax = [Link]()
[Link](X, Y) Z = [Link](0, 1, (100, 3))
[Link]() Label (everything)
[Link](Z)
4 Observe A Sine wave
[Link](X, Y)
1.0 [Link](None)
0.5 Tweak ax.set_title(”A Sine wave”)
0.0
0.5 You can modify pretty much anything in a plot, including lim-
[Link](X, Y)
1.0 its, colors, markers, line width and styles, ticks and ticks la-
0 5 10 15 20 25 30 ax.set_ylabel(None)
bels, titles, etc.
ax.set_xlabel(”Time”)
Time
Choose X = [Link](0, 10, 100)
Y = [Link](X) Explore
Matplotlib offers several kind of plots (see Gallery): [Link](X, Y, color=”black”)
Figures are shown with a graphical user interface that al-
X = [Link](0, 1, 100) X = [Link](0, 10, 100) lows to zoom and pan the figure, to navigate between the
Y = [Link](0, 1, 100) Y = [Link](X) different views and to show the value under the mouse.
[Link](X, Y) [Link](X, Y, linestyle=”--”)
Save (bitmap or vector format)
X = [Link](10) X = [Link](0, 10, 100)
Y = [Link](1, 10, 10) Y = [Link](X)
[Link](X, Y) [Link](X, Y, linewidth=5) [Link](”[Link]”, dpi=300)
[Link](”[Link]”)
Z = [Link](0, 1, (8, 8)) X = [Link](0, 10, 100)
Y = [Link](X)
Matplotlib 3.9.4 handout for beginners. Copyright (c) 2021 Matplotlib Development
[Link](Z) [Link](X, Y, marker=”o”) Team. Released under a CC-BY 4.0 International License. Supported by NumFOCUS.