matplotlib

matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell, web application servers, and six graphical user interface toolkits.

Policy

matplotlib is freely available to users at HPC2N.

Citations

If Matplotlib contributes to a project that leads to a scientific publication, please acknowledge this fact by citing J. D. Hunter, “Matplotlib: A 2D Graphics Environment”, Computing in Science & Engineering, vol. 9, no. 3, pp. 90-95, 2007.

For more information about citing matplotlib (bibtex file etc.) see the Citing matplotlib page.

Overview

Matplotlib is one of the most popular and flexible function libraries for data visualization in use today.

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.

  • Create publication quality plots.
  • Make interactive figures that can zoom, pan, update.
  • Customize visual style and layout.
  • Export to many file formats.
  • Embed in JupyterLab and Graphical User Interfaces.
  • Use a rich array of third-party packages built on Matplotlib.

Usage at HPC2N

On HPC2N we have matplotlib available as a module.

Loading

To use the matplotlib module, add it to your environment. You can find versions with

module spider matplotlib

and you can then find how to load a specific version (including prerequisites), with

module spider matplotlib/<VERSION> 

Example, loading matplotlib 3.8.2

module load GCC/13.2.0 matplotlib/3.8.2 

Running

You can use matplotlib both in batchjobs and directly, in for instance Python.

Note that you need to either login with ThinLinc or ssh -X or ssh -Y in order to open a display.

Note

At the regular terminal, Matplotlib figures will typically not display unless you a set the backend that allows displays and is compatible with your version of Python. Backends are engines for either displaying figures or writing them to image files.

For Python 3.11.x, Tkinter is the backend that generates figure popups when you create a plot and then type plt.show() at the Python command line.

First you need to load the Tkinter module in a version compatible with your matplotlib. The easiest way to do this is to just load matplotlib and its prerequisite, then type module load Tkinter/ and hit TAB. It will autocomplete to a suitable version.

Then, in Python, you can then set the backend by importing the top-level matplotlib package and then running matplotlib.use('Tkinter') or matplotlib.use('TkAgg') before doing any plotting (if you forget, you can set it at any time).

If for some reason that doesn’t work, you can try matplotlib.use('Qt5Agg').

Example - click to reveal

Login, load the modules, and start Python (or run a script:

$ ssh -X <username>@kebnekaise.hpc2n.umu.se
$ module load GCC/12.3.0 Python/3.11.3 SciPy-bundle/2023.07 matplotlib/3.7.2 Tkinter/3.11.3
$ python      

Simple example to show how to use it with matplotlib:

>>> import matplotlib
>>> matplotlib.use('TkAgg')
>>> 
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> dataframe = pd.read_csv("scottish_hills.csv")
>>> x = dataframe.Height
>>> y = dataframe.Latitude
>>> plt.scatter(x, y)
>>> plt.show()
>>> 

Note

In Jupyter, after importing matplotlib or any of its sub-modules, you typically need to add % matplotlib inline before you make any plots. You should not need to set matplotlib.use().

There is more information at the page from a recent Python course.

Batch script examples

As mentioned, it is possible to run matplotlib through a batch job. Of course, you cannot open any displays while doing so, which means you need to save the plots as images.

Example

We will use the same simple example as further up, with the exception that we now do plt.savefig("myplot.png") instead of displaying the plot directly.

#!/bin/bash
#SBATCH -A <project-id>
#SBATCH --time=00:05:00 # Asking for 5 minutes. HHH:MM:SS - change to what you need 
#SBATCH -n 1 # Asking for 1 core as it is serial 

# Load any modules you need, here for Python 3.11.3
ml GCC/12.3.0 Python/3.11.3 SciPy-bundle/2023.07 matplotlib/3.7.2 Tkinter/3.11.3

# Run your Python script
python pandas_matplotlib-batch-kebnekaise.py
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('TkAgg')

dataframe = pd.read_csv("scottish_hills.csv")
x = dataframe.Height
y = dataframe.Latitude
plt.scatter(x, y)
plt.savefig("myplot.png")

Additional info

More information can be found on