Skip to content

Working with graphics

The minimal plot

Learning outcomes

  • Practice using the documentation of your HPC cluster
  • Create a minimal plot
  • Load the Matplotlib module
  • (optional) View the plot
For teachers

Teaching goals are:

  • Learners hear about matplotlib
  • Learners may find out again that X-forwarding is important
  • Learners again have gotten 'stuff to work'

Lesson plan:

  • 5 mins: prior knowledge
  • 5 mins: presentation
  • 15 mins: challenge
  • 5 mins: feedback

Prior:

  • What is meant with a plotting library?
  • What is meant with a plotting package/module?
  • Can you name a Python plotting package?

Why use graphics?

Graphics are useful to visualize your data, e.g. in the form of plots.

Instead of creating plots on your local computer, in this session, we will create plots on an HPC cluster.

Matplotlib is a popular Python package to make plots. Here we experience how easy/hard it is to use Matplotlib on your favorite HPC cluster. The hardest part will be to find out which software modules to load.

For this exercise, we will use this code:

import matplotlib.pyplot as plt

plt.plot([0, 1, 4, 9, 16])
plt.savefig("my_plot.png")

This Python code will create a minimal plot and save it to file.

Viewing graphics from a terminal may not always work. There are two solutions for this:

  • Login to the remote desktop environment of your HPC cluster: a remote desktop environment has graphics enabled
  • Login via SSH with X-forwarding enabled: this allows displaying simple graphics

In the worst case, downloading the image to your local computer is a clumsy option that works too.

Demonstration

Before doing the exercises, a teacher will speedrun through the exercises.

Exercises

Enjoy a video?

You can find a video with solutions to these exercises:

HPC Cluster YouTube video
Arrhenius HPC YouTube video
Bianca Not needed: no registrations
COSMOS YouTube video
Dardel YouTube video
Dardel Failing using SSH: YouTube video
Kebnekaise YouTube video
LUMI Not needed: no registrations
Pelle YouTube video
How difficult will this exercise be?

The difficulty of these exercises depends on the documentation of your HPC cluster:

HPC cluster name Environment Documentation How to view images? Difficulty
Arrhenius HPC Remote desktop To be accepted eog Easy
Bianca Console Excellent Copy to local computer Medium
COSMOS Console None Present Medium
Dardel Console None ImageMagick Impossible?
Dardel Remote desktop None ImageMagick Medium
LUMI Console None Unknown Unknown
Kebnekaise Console Excellent ImageMagick Easy
Pelle Console Excellent eog Easy

Exercise 1: Use Matplotlib to create a plot

Create a Python script called create_plot.py with the content of the example code in it.

Answer

We did this in the previous session, 'Working with Python scripts'.

HPC Cluster How to run nano
Arrhenius HPC nano create_plot.py
Bianca nano create_plot.py
COSMOS nano create_plot.py
Dardel module load nano ; nano create_plot.py
Kebnekaise nano create_plot.py
LUMI nano create_plot.py
Pelle nano create_plot.py

Run the Python script (i.e. without loading the Matplotlib module) and observe the error. The error should be mentioning Matplotlib (if not: the 'Troubleshooting' section below the answer shows how to fix this)

Answer

Loading the Python module again:

HPC Cluster Documentation
Arrhenius module load Python/3.13.5-bundle-SciPy-2025.07-mpi4py-4.1.0-gcc-2025b-eb
Bianca module load python/3.12.7
COSMOS module load GCCcore/13.2.0 Python/3.11.5
Dardel module load PDC python/3.12.3
Kebnekaise module load GCC/12.3.0 Python/3.11.3
LUMI module load cray-python/3.11.7
Pelle module load Python/3.13.5-GCCcore-14.3.0

Now you can run this Python script in the shell by:

HPC Cluster How to run hello_world.py
Arrhenius HPC python hello_world.py
Bianca python hello_world.py
COSMOS python hello_world.py
Dardel python hello_world.py
Kebnekaise python hello_world.py
LUMI python hello_world.py
Pelle python hello_world.py
Troubleshooting

These are the errors that may occur:

Error Solution
No module named 'matplotlib' Load the Matplotlib module, which is the next step
Cannot find Python Load a Python module, as done in previous session
Core dumped Login to Dardel using ThinLinc

The complete error message to get is similar to:

[richel@pelle2 ~]$ python create_plot.py 
Traceback (most recent call last):
  File "/domus/h1/richel/create_plot.py", line 1, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

Now we have created the Python script, we now need to get Matplotlib to work.

Go to the user documentation for your favorite HPC cluster:

HPC cluster User documentation
Arrhenius User documentation
Bianca User documentation
COSMOS User documentation
Dardel User documentation
Kebnekaise User documentation
LUMI User documentation
Pelle User documentation

For your favorite HPC center, find the documentation on how to use Matplotlib. Search for a maximum of 5 minutes! If you cannot find it, (try to) apply the steps in the UPPMAX documentation about Matplotlib for a maximum of 5 minutes! After that, simply copy-paste the answer of the next question.

Answer

From the documentation of your center, searching for, for example, 'Matplotlib'.

HPC Cluster Documentation
Arrhenius HPC Documentation
Bianca Not needed: no registrations
COSMOS ⚠ Closest documentation
Dardel ⚠ None
Kebnekaise Documentation
LUMI Not needed: no registrations
Pelle Documentation
  • ⚠ means that the documentation does not clearly answer this question. You may find that you can piece it together easily enough or you may find that you cannot. You are encouraged to contact your HPC center to help them help you better
  • If there is good documentation: load the software module needed. Else, copy-paste the answer.
Answer

HPC Cluster Documentation
Arrhenius HPC module load buildtool-easybuild/5.2.1-hpca3ef7d197 GCC/14.3.0 matplotlib/3.10.5
Bianca Not needed: no registrations
COSMOS module load GCC/13.3.0 matplotlib/3.9.2
Dardel module load python/3.12.3 ; pip3 install matplotlib
Kebnekaise module load GCC/13.2.0 matplotlib/3.8.2
LUMI Not needed: no registrations
Pelle module load matplotlib

Exercise 2: (optional) View the plot

This exercise is optional, as it has nothing to do with Python. However, it is quite practical to be able to see your plot directly.

  • Login to your HPC cluster in a way that allows for graphics (if you have not done so already). There are two ways:

    • Login to the remote desktop environment of your HPC cluster: a remote desktop environment has graphics enabled. You can find an overview at this course's FAQ 'How can I login to an HPC cluster'.
    • Login via SSH with X-forwarding enabled: this allows displaying simple graphics. In short: use ssh -X [username]@[hpc_cluster] (instead of omitting the -X). This may already work or this may never work.
  • View the plot:

    • In a remote desktop environment, this is straightforward: click on the image from a file explorer
    • When using SSH with X-forwarding enabled: use the way as shown below in the table.

How display an image in SSH with X-forwarding enabled

HPC Cluster How to display an image
Arrhenius HPC eog my_plot.png
Bianca Impossible: X-forwarding is disabled. Use a remote desktop.
COSMOS eog my_plot.png
Dardel module load imagemagick/7.1.1-41 ; display my_plot.png
Kebnekaise module load GCCcore/13.2.0 ImageMagick/7.1.1-34 ; display my_plot.png
LUMI Not needed: no registrations
Pelle eog my_plot.png
What is the error when I do not have X-forwarding enabled?

Here is one possible error message:

[x_ricbi@tetralith3 ~]$ eog my_plot.png

(eog:2405120): Gtk-WARNING **: 19:46:31.288: cannot open display: 
```bash

Here is another possible error message:

```bash
display: unable to open X server `' @ error/display.c/DisplayImageCommand/413.
How does eog display a plot?

Here is how eog displays the plot:

The plot shown with eog

How does display display a plot?

In a remote desktop environment, this looks like:

The minimal plot on Arrhenius HPC

Done?

You are done for this morning!

You are welcome to stay, but you may also leave.

See you today at 13:00 if you want to learn the basics of Python programming 👍