Getting started with matplotlib
matplotlib is a plotting library for Python. It provides object-oriented APIs for embedding plots into applications. It is similar to MATLAB in capacity and syntax.
It was originally written by J.D.Hunter and is actively being developed. It is distributed under a BSD-Style License.
|Version||Python Versions Supported||Remarks||Release Date|
|1.3.1||2.6, 2.7, 3.x||Older Stable Version||2013-10-10|
|1.4.3||2.6, 2.7, 3.x||Previous Stable Version||2015-07-14|
|1.5.3||2.7, 3.x||Current Stable Version||2016-01-11|
|2.x||2.7, 3.x||Latest Development Version||2016-07-25|
Customizing a matplotlib plot
Imperative vs. Object-oriented Syntax
Matplotlib supports both object-oriented and imperative syntax for plotting. The imperative syntax is intentionally designed to be very close to Matlab syntax.
The imperative syntax (sometimes called 'state-machine' syntax) issues a string of commands all of which act on the most recent figure or axis (like Matlab). The object-oriented syntax, on the other hand, explicitly acts on the objects (figure, axis, etc.) of interest. A key point in the zen of Python states that explicit is better than implicit so the object-oriented syntax is more pythonic. However, the imperative syntax is convenient for new converts from Matlab and for writing small, "throwaway" plot scripts. Below is an example of the two different styles.
Both examples produce the same plot which is shown below.
Installation and Setup
There are several ways to go about installing matplotlib, some of which will depend on the system you are using. If you are lucky, you will be able to use a package manager to easily install the matplotlib module and its dependencies.
On Windows machines you can try to use the pip package manager to install matplotlib. See here for information on setting up pip in a Windows environment.
If you cannot use pip for whatever reason, then try to install from source.
Ideally, the system package manager or pip should be used to install matplotlib, either by installing the
python-matplotlib package or by running
pip install matplotlib.
If this is not possible (e.g. you do not have sudo privileges on the machine you are using), then you can install from source using the
python setup.py install --user. Typically, this will install matplotlib into
sudo apt-get install python-matplotlib
sudo yum install python-matplotlib
See the matplotlib website for advice on how to fix a broken matplotlib.
Two dimensional (2D) arrays
Display a two dimensional (2D) array on the axes.