Scientific Visualisations with Python

Python

Python is a popular, versatile, and beginner-friendly programming language known for its simplicity and readability. It is widely used for web development, data analysis, artificial intelligence, and more.

You can download Python from the official website .

Installation is straightforward: download the appropriate version for your operating system and follow the installation instructions, ensuring you add Python to your system's PATH during setup.

Text editor

To write and run Python programs, you need a text editor or an integrated development environment (IDE). A text editor allows you to write and save source code, while an IDE typically provides additional tools, such as code completion, debugging, and project management.

A recommended text editor for beginners is Sublime Text. It is lightweight, fast, and supports Python syntax highlighting, making Python code easier to read and edit.

You can download Sublime Text from its official website: .

Libraries

Python libraries are collections of pre-written functions and tools that extend the capabilities of the language. Instead of implementing every operation from scratch, users can import existing libraries and use their functionality directly.

Matplotlib

Matplotlib is one of the most widely used Python libraries for data visualization. It provides tools for creating two-dimensional plots, including line graphs, scatter plots, bar charts, histograms, and contour plots.

pip install matplotlib
https://matplotlib.org

Seaborn

Seaborn is built on top of Matplotlib and simplifies the creation of statistical visualizations. It provides a high-level interface for creating heatmaps, regression plots, distribution plots, and categorical visualizations. It is particularly useful when analyzing large datasets or comparing statistical properties across multiple groups.

pip instal seaborn
https://seaborn.pydata.org/

Bokeh

Bokeh is a library for creating interactive and web-based visualizations. It supports interactive plots, zooming, panning, tooltips, and dashboards. It is particularly useful when users need to explore datasets interactively or present results through a web browser.

pip install bokeh
https://bokeh.org/

Plotly

Plotly is a Pyton library for creating interactive, web-based visualizations. It supports line plots, scatter plots, bar charts, heatmaps, and three-dimensional visualizations. Plotly allows users to zoom, pan, rotate 3D plots, and inspect data points using interactive tooltips. It is particularly useful for scientific data analysis and presenting results in an interactive format.

pip install plotly
https://plotly.com/

PyVista

Make 3D analysis and visualization feel like the rest of scientific Python, so you can focus on the research instead of wrangling spatial data structures and 3D rendering.

https://pyvista.org

PyVtk

VTK (Visualization Toolkit); is a powerful toolkit for scientific visualization, particularly for three-dimensional data, surface meshes, volumetric datasets, and simulation results. It is widely used in scientific computing and engineering to process and visualize complex geometries and fields.

pip install vtk
https://vtk.org/

OpenGl

OpenGL is not a plotting library in the same sense as Matplotlib. Instead, it provides lower-level graphics functionality. PyOpenGL can be useful when developing custom visualization applications, interactive 3D environments, or specialized rendering systems. It generally requires more programming effort than higher-level visualization libraries.

pip install pyopengl
https://www.opengl.org/

Vedo

Vedo offers intuitive tools for creating, rendering, and interacting with 3D objects and simulations. It is built on top of VTK (Visualization Toolkit) and simplifies tasks like mesh manipulation, plotting, and animations.

pip install vedo
https://vedo.embl.es/

Folium

Folium makes easy to visualize data that’s been manipulated in Python on an interactive leaflet map. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map. The library has a number of built-in tilesets from OpenStreetMap, Mapbox, etc, and supports custom tilesets. Folium supports both Image, Video, GeoJSON and TopoJSON overlays and has a number of vector layers built-in.

pip install folium
https://pypi.org/project/folium/

VisPy

VisPy is a high-performance library designed for 2D and 3D visualizations. It is optimized for rendering large datasets efficiently using GPU acceleration, making it ideal for computationally intensive applications.

pip install vispy
https://vispy.org/

Example

If Python and the required libraries have already been installed, you can create your first visualization. In this example, we will use Sublime Text to write a Python script and Matplotlib to generate a simple line plot.

• Open Sublime Text.
• Create a new file and copy the code shown below.
• Save the file as example.py. Make sure that the file has the .py extension.
• Open a terminal or command prompt in the directory where the file is saved.
• Run the script by typing python example.py.

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 8, 5]
plt.plot(x, y)
plt.show()

After running the script, a window containing a line plot should appear. The horizontal axis represents the values stored in x, while the vertical axis represents the values stored in y. The markers indicate the individual data points, and the line connects them.

This basic plot can also be transformed into a more polished, publication-quality visualization by customizing its appearance, including the colors, markers, line styles, labels, fonts, and overall layout.

However, creating a more refined figure typically requires additional code to control these elements and achieve the desired presentation.

import matplotlib.pyplot as plt
import numpy as np
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 8, 5]
plt.rcParams.update({
    "font.family": "DejaVu Sans",
    "font.size": 11,
    "axes.titlesize": 19,
    "axes.titleweight": "bold",
    "axes.labelsize": 12,
    "xtick.labelsize": 10,
    "ytick.labelsize": 10,
    "axes.spines.top": False,
    "axes.spines.right": False,
    "axes.edgecolor": "#C9D2DC",
    "axes.linewidth": 0.9,
    "xtick.color": "#526174",
    "ytick.color": "#526174",
    "axes.labelcolor": "#344563",
    "text.color": "#172B4D",
    "savefig.dpi": 300,
    "savefig.bbox": "tight",
})
fig, ax = plt.subplots(
    figsize=(7, 5.4),
    facecolor="#F7F9FC"
)
ax.set_facecolor("#F7F9FC")
ax.plot(
    x, y,
    color="#2864DC",
    linewidth=3,
    marker="o",
    markersize=9,
    markerfacecolor="white",
    markeredgecolor="#2864DC",
    markeredgewidth=2.5,
    solid_capstyle="round",
    zorder=3,
)
imax = int(np.argmax(y))
ax.scatter(
    x[imax], y[imax],
    s=150,
    color="#F08A4B",
    edgecolor="white",
    linewidth=1.8,
    zorder=5,
)
ax.annotate(
    f"Maximum: {y[imax]}",
    xy=(x[imax], y[imax]),
    xytext=(14, -5),
    textcoords="offset points",
    fontsize=10,
    fontweight="bold",
    color="#B85B25",
    va="center",
)
fig.suptitle(
    "Change in Values Across Measurements",
    x=0.105,
    y=0.97,
    ha="left",
    fontsize=19,
    fontweight="bold",
    color="#172B4D",
)
ax.set_title(
    "Illustrative dataset · 5 observations",
    loc="left",
    fontsize=10,
    fontweight="normal",
    color="#718096",
    pad=18,
)
ax.set_xlabel("Measurement Number", labelpad=12)
ax.set_ylabel("Value", labelpad=12)
ax.set_xticks(x)
ax.set_ylim(0, 9.5)
ax.set_yticks(np.arange(0, 10, 2))
ax.margins(x=0.08)
ax.grid(
    axis="y",
    color="#DCE3EC",
    linewidth=0.9,
)
ax.grid(axis="x", visible=False)
ax.set_axisbelow(True)
fig.text(
    0.105,
    0.025,
    "Source: illustrative data",
    fontsize=8.5,
    color="#718096",
)
fig.subplots_adjust(
    left=0.105,
    right=0.96,
    top=0.78,
    bottom=0.17,
)
fig.savefig(
    "../imgs/publication_plot.png",
    dpi=300,
    bbox_inches="tight",
    facecolor=fig.get_facecolor(),
)
plt.show()