How to use matplotlib for scientific plotting on Linux

If you want an efficient, automatable solution for producing high-quality scientific plots in Linux, then consider using matplotlib. Matplotlib is a Python-based open-source scientific plotting package with a license based on the Python Software Foundation license. The extensive documentation and examples, integration with Python and the NumPy scientific computing package, and automation capability are just a few reasons why this package is a solid choice for scientific plotting in a Linux environment. This tutorial will provide several example plots created with matplotlib.

Features

  • Numerous plot types (bar, box, contour, histogram, scatter, line plots...)
  • Python-based syntax
  • Integration with the NumPy scientific computing package
  • Source data can be Python lists, Python tuples, or NumPy arrays
  • Customizable plot format (axes scales, tick positions, tick labels...)
  • Customizable text (font, size, position...)
  • TeX formatting (equations, symbols, Greek characters...)
  • Compatible with IPython (allows interactive plotting from a Python shell)
  • Automation - use Python loops to iteratively create plots
  • Save plots to image files (png, pdf, ps, eps, and svg format)

The Python-based syntax of matplotlib serves as the foundation for many of its features and enables an efficient workflow. There are many scientific plotting packages that can produce quality plots, but do they allow you to do it directly from within your Python code? On top of that, do they allow you to create automated routines for iterative creation of plots that can be saved as image files? Matplotlib allows you to accomplish all of these tasks. You can now look forward to saving time that would have otherwise been spent manually creating multiple plots.

Installation

Installation of Python and the NumPy package is a prerequisite for use of matplotlib. Instructions for installing NumPy can be found here.

To install matplotlib in Debian or Ubuntu, run the following command:

$ sudo apt-get install python-matplotlib

To install matplotlib in Fedora or CentOS/RHEL, run the following command:

$ sudo yum install python-matplotlib

Matplotlib Examples

This tutorial will provide several plotting examples that demonstrate how to use matplotlib:

  • Scatter and line plot
  • Histogram plot
  • Pie chart

In these examples we will use Python scripts to execute matplotlib commands. Note that the numpy and matplotlib modules must be imported from within the scripts via the import command. np is specified as a reference to the numpy module and plt is specified as a reference to the matplotlib.pyplot namespace:

import numpy as np
import matplotlib.pyplot as plt

Example 1: Scatter and Line Plot

The first script, script1.py completes the following tasks:

  • Creates three data sets (xData, yData1, and yData2)
  • Creates a new figure (assigned number 1) with a width and height of 8 inches and 6 inches, respectively
  • Sets the plot title, x-axis label, and y-axis label (all with font size of 14)
  • Plots the first data set, yData1, as a function of the xData dataset as a dotted blue line with circular markers and a label of "y1 data"
  • Plots the second data set, yData2, as a function of the xData dataset as a solid red line with no markers and a label of "y2 data".
  • Positions the legend in the upper left-hand corner of the plot
  • Saves the figure as a PNG file

Contents of script1.py:

import numpy as np
import matplotlib.pyplot as plt

xData = np.arange(0, 10, 1)
yData1 = xData.__pow__(2.0)
yData2 = np.arange(15, 61, 5)
plt.figure(num=1, figsize=(8, 6))
plt.title('Plot 1', size=14)
plt.xlabel('x-axis', size=14)
plt.ylabel('y-axis', size=14)
plt.plot(xData, yData1, color='b', linestyle='--', marker='o', label='y1 data')
plt.plot(xData, yData2, color='r', linestyle='-', label='y2 data')
plt.legend(loc='upper left')
plt.savefig('images/plot1.png', format='png')

The resulting plot is shown below:

Example 2: Histogram Plot

The second script, script2.py completes the following tasks:

  • Creates a data set containing 1000 random samples from a Normal distribution
  • Creates a new figure (assigned number 1) with a width and height of 8 inches and 6 inches, respectively
  • Sets the plot title, x-axis label, and y-axis label (all with font size of 14)
  • Plots the data set, samples, as a histogram with 40 bins and an upper and lower bound of -10 and 10, respectively
  • Adds text to the plot and uses TeX formatting to display the Greek letters mu and sigma (font size of 16)
  • Saves the figure as a PNG file

Contents of script2.py:

import numpy as np
import matplotlib.pyplot as plt

mu = 0.0
sigma = 2.0
samples = np.random.normal(loc=mu, scale=sigma, size=1000)
plt.figure(num=1, figsize=(8, 6))
plt.title('Plot 2', size=14)
plt.xlabel('value', size=14)
plt.ylabel('counts', size=14)
plt.hist(samples, bins=40, range=(-10, 10))
plt.text(-9, 100, r'$\mu$ = 0.0, $\sigma$ = 2.0', size=16)
plt.savefig('images/plot2.png', format='png')

The resulting plot is shown below:

Example 3: Pie Chart

The third script, script3.py completes the following tasks:

  • Creates data set containing five integers
  • Creates a new figure (assigned number 1) with a width and height of 6 inches and 6 inches, respectively
  • Adds an axes to the figure with an aspect ratio of 1
  • Sets the plot title (font size of 14)
  • Plots the data set, data, as a pie chart with labels included
  • Saves the figure as a PNG file

Contents of script3.py:

import numpy as np
import matplotlib.pyplot as plt

data = [33, 25, 20, 12, 10]
plt.figure(num=1, figsize=(6, 6))
plt.axes(aspect=1)
plt.title('Plot 3', size=14)
plt.pie(data, labels=('Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5'))
plt.savefig('images/plot3.png', format='png')

The resulting plot is shown below:

Summary

This tutorial provides several examples of plots that can be created with the matplotlib scientific plotting package. Matplotlib is a great solution for scientific plotting in a Linux environment given its natural integration with Python and NumPy, its ability to be automated, and its production of a wide variety of customizable high quality plots. Documentation and examples for the matplotlib package can be found here.


Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Joshua Reed is a graduate student and IT programming intern. He loves solving challenging problems by combining the realms of Linux, physics, programming, and software. In his free time he enjoys playing guitar and listening to rock, chillstep, and trance music.

5 thoughts on “How to use matplotlib for scientific plotting on Linux

  1. Great article. My question is how to use this package with data in a spreadsheet of database? I have a lot of data that I want to plot, but the plotting functions of spreadsheets and databases usually don't have what I want. For example, a plot with two scales on the alternate or primary y-axis.

    Thanks,
    Dave

  2. Agree with the author. I've used matplotlib for my MSc thesis for data presentation. It integrates well with LaTeX, and produces excellent, professionally looking SVGs - you can zoom the resulting PDF to maximum without loosing quality. The ability to automate the process is just great, simple script and you can generate/refresh all the data in no time.

    @Dave
    try exporting your file to CSV which is easy to import within the script. I used the same way for my thesis plots. Main simulation program produced CSV files which were fed to my python script (trough command line args) using another bash script, then the results were saved in a given LaTeX source location.

  3. Thanks Dave and Matt for your positive feedback!

    @Dave
    I agree with Matt - exporting your data to a comma-delimited text file and then importing this into Python (via native Python file I/O functions or by using the NumPy loadtxt() function) is probably easiest. You can create a plot with two scales using the matplotlib twinx() or twiny() functions (there's a good example here: http://matplotlib.org/examples/api/two_scales.html)

    @Matt
    As a follow-up to your LaTeX comment: for those who use the LyX document processor - I've found that importing matplotlib-generated EPS image files works (and looks) great. In general, you can't go wrong when combining LaTeX/LyX with matplotlib-generated plots for a thesis or other professional documents.

  4. @Dave
    You can check out my script at: https://gist.github.com/anonymous/97808a1b60a7b91ecb78

    The CSV has to have this kind of format (first line contains a header):
    $M_{2} / C_{1} \quad SE$;$E_b / N_0 \quad [dB]$;$SE\quad[bit/s/Hz]$;$SISO$;$MIMO_{2\times2}$;$MIMO_{2\times3}$;$MIMO_{2\times4}$;$MIMO_{3\times4}$
    EbN0dB;SE1x1;SE2x2;SE2x3;SE2x4;SE3x4
    -5.00;0.000000;0.203683;0.500694;0.668739;0.880412
    -4.00;0.022256;0.000000;0.611733;0.940776;1.232834
    -3.00;0.000000;0.000000;0.796890;0.940776;1.307444
    -2.00;0.091416;0.546888;0.889530;1.259781;1.582056
    -1.00;0.000000;0.565476;0.906801;1.509456;1.787927

    Examples on how to run it:
    ./plot.py -i m1_ber.csv -o m1_ber.eps -s 0
    ./plot.py -i m1_fer.csv -o m1_fer.eps -s 0
    ./plot.py -i m1_se.csv -o m1_se.eps -log 0 -s 0 -loc 4

    Hope it will be useful.

  5. @Dave, use Pandas. You can plot from the DataFrame object and it can also load from csv or db query amongst other things. There are a lot of tutorials on the Pandas website. Running this stuff in IPython is normally the easiest way. Alternatively download R and RStudio.

Leave a comment

Your email address will not be published. Required fields are marked *