Matplotlib Tutorial - Introduction and Installation
Matplotlib is the most widely used Python module to plot graphs. It could produce publication-ready figures easily and be used in different platforms.
pyplot
module of Matplotlib has a MATLAB-like interface, therefore it is easier to use pyplot
if you are already user of MATLAB.
Install Matplotlib
Windows
- pip
pip install matplotlib
This pip
command also installs the Matplotlib dependencies automatically.
- Install Wheel File
You could also download the unofficial pre-built 32 or 64-bit Windows Matplotlib wheel file of Python 2.7 or Python 3.4+ version from the famous site of Unofficial Windows Binaries for Python Extension Packages.
After the wheel file is downloaded, use the command below to install the wheel file.
pip install matplot_wheel_file_path
Refer to the wheel file installation guide here.
Linux
Open the Linux terminal and use the command below according to the Python version in your system.
Python 2.7
sudo apt-get install python-matplotlib
Python 3.4+
sudo apt-get instlal python3-matplotlib
Matplotlib Hello World
Now we will take the first bite of Matplotlib.
from matplotlib import pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
This example plots the graph of a straigt line that has the 2-D data - [1,2,3],[4,5,6]
.
from matplotlib import pyplot as plt
As we mentioned above, pyplot
is a MATLAB-like plotting module.
plt.plot([1, 2, 3], [4, 5, 6])
It plots the x
and y
using default line and color style because no extra argument is passed here.
plt.show()
It displays the figure. The plotting figure will not be shown before this line is executed.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook