Scientific Visualisations with Python

Figure

In Matplotlib, a Figure is the top-level container that holds all the elements of a visualization. These include one or more Axes objects, titles, axis labels, legends, annotations, and other graphical elements.

A figure can be thought of as a canvas on which the visualization is constructed. Its individual components can be customized independently, including their size, position, colors, opacity, line width, line style, font properties, spacing, and margins.

The size of a figure is specified by its width and height in inches using the figsize parameter. For example, figsize=(8, 6) creates a figure that is 8 inches wide and 6 inches tall.

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 6))
plt.savefig("filename.png")

A figure can contain multiple subplots, which is particularly useful when comparing different datasets or presenting several related analyses in a single scientific figure. The following illustration shows the main components of a Matplotlib figure.

Anatomy of matplotlib figure

DPI and image resolution

DPI stands for dots per inch. In Matplotlib, it describes the number of pixels per inch used when rendering a figure to a raster image.

The relationship between figure dimensions, DPI, and output resolution is:

$$\text{Pixel dimensions} = \text{Figure dimensions in inches} \times \text{DPI}$$

For example, a figure with dimensions of 8 × 6 inches saved at 300 DPI produces a raster image of 2400 × 1800 pixels.

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 6))
plt.savefig("filename.png", dpi=300)

Increasing DPI increases the pixel dimensions of raster output, which can improve the rendering of fine details. However, DPI alone does not determine the quality of a figure. Readability also depends on its physical dimensions, font sizes, line widths, and the complexity of the visualization.

For vector formats such as PDF and SVG, text, lines, and paths can remain resolution-independent. DPI still matters for rasterized elements and embedded images.

Units used in figure design

Matplotlib uses several units for different aspects of figure design. Understanding their relationships helps when preparing figures for scientific articles.

Unit Meaning Typical use
Inch (in) 1 inch = 25.4 mm Figure width and height (figsize)
Millimetre (mm) 1 mm = 1/25.4 inch Journal column dimensions and physical layout
Point (pt) 1 pt = 1/72 inch Font sizes and many line-width settings
Pixel (px) A single raster image element Output image dimensions
DPI Dots per inch; commonly used as pixels per inch in raster output Raster image resolution

For example, Matplotlib's figsize is specified in inches, whereas font sizes are typically specified in points. Raster image dimensions in pixels depend on both the physical figure size and the selected DPI. It is important to distinguish physical size from pixel dimensions. Two images can have the same pixel dimensions but different intended physical sizes, or the same physical dimensions but different raster resolutions.

Figure formats

Matplotlib supports several output formats, including PNG, JPEG, PDF, SVG, and EPS. The choice of format depends on how the figure will be used.

Format Type Advantages Typical applications
PNG Raster Lossless compression; supports transparency Presentations, web content, raster submissions
JPEG Raster Small file sizes for continuous-tone images Photographic content; usually less suitable for plots
PDF Primarily vector Scalable text and graphics; widely accepted in publishing Scientific articles and printing
SVG Vector Scalable and editable in compatible software Web publishing and further graphic editing
EPS Vector-oriented Supported by some traditional publishing workflows Latex

Figures can be exported using savefig() :

fig.savefig("figure.png", dpi=300)
fig.savefig("figure.pdf")
fig.savefig("figure.svg")

For scientific plots consisting mainly of lines, markers, text, and shapes, PDF or SVG is often a good choice because these elements can remain sharp when enlarged. PNG is useful when a raster image is required.

Always check the target journal's submission guidelines before selecting the final format.

Setup suitable for scientific publication

To ensure the figure is suitable for scientific publication, it's crucial to define the correct resolution and font size. For high-quality images, a dpi of 300 is recommended. Additionally, you can adjust the font size of titles, labels, and legends by specifying the fontsize parameter within plot functions.

import matplotlib.pyplot as plt
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.size"] = 12
fig = plt.figure(figsize=(4, 3))
ax = fig.add_subplot(111)
x = [1,2,3,4,5]
y = [2,5,1,7,6]
ax.plot(x,y, label="plot")
ax.set_xlabel("x-label")
ax.set_ylabel("y-label")
ax.set_title("title")
ax.legend()
plt.tight_layout()
plt.savefig("../../data/example_article/filename.pdf")

Here is the figure embedded in the sample article.

Single-column or double-column layout

Before creating a figure, determine how it will be placed in the article. Scientific journals commonly use single-column figures, double-column figures, or figures that span the full text width.

A single-column figure is suitable for a compact plot that can be understood independently. A double-column figure provides more space for multiple panels, detailed legends, or complex visualizations.