Scientific Visualisations with Python

What is 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

https://www.python.org.

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.

To write and run Python programs, you need a text editor, such as Sublime Text, Visual Studio Code, or even Python's built-in IDLE. Save your code with a .py extension and run it using the command line by typing python filename.py. A simple "Hello, World!" program in Python looks like this:

print("Hello, World!")

This program outputs "Hello, World!" to the console, demonstrating Python's simplicity.

How to use Python as a tool for visualisation

To use Python for visualization, you'll need a library like Matplotlib, a popular package for creating 2D plots and graphs. Python includes a package manager called pip, which simplifies installing additional libraries. If Python and pip are already installed, you can open a terminal or command prompt and type the following command to install Matplotlib:

pip install matplotlib

Once installed, you can use Matplotlib to generate plots by writing Python scripts. For example, to create a simple line graph, you can write the following code in your text editor and save it as a .py file:

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

Running this script will display a window with a line graph.

Ohter Python librieries

Although Matplotlib is often regarded as the standard Python library for visualization, many other libraries offer unique capabilities and are tailored to specific use cases. Here is a list of some of the most popular Python visualization libraries:

Seaborn Built on top of Matplotlib, Seaborn simplifies the creation of complex visualizations with high-level APIs. It is particularly well-suited for statistical data visualization, making it a favorite for creating heatmaps, regression plots, and categorical visualizations.

pip instal seaborn

Bokeh excels in generating interactive and web-ready visualizations. Its ability to handle large datasets and create dashboards makes it a great choice for data exploration and presentation.

pip instal bokeh

PyVtk provides tools for generating visualizations compatible with the Visualization Toolkit (VTK). It is frequently used in scientific computing to render 3D graphics and handle volumetric data.

pip install vtk

OpenGl

pip install pyopengl

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

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

Each of these libraries offers unique features that cater to different visualization needs, allowing users to choose the best tool for their specific project.

Interactive Coding with Python in Jupyter

Installation

Jupyter lab provides an interactive environment for writing and running Python code, especially useful for data analysis and visualizations. They allow you to write and execute code in cells, providing immediate feedback and a rich interface for working with Python. To use Jupyter notebooks for interactive coding, you need to install the Jupyter:

pip install jupyter

Type the following command to start Jupyter:

jupyter lab

This command will open the Jupyter Lab interface in your default web browser (typically at http://localhost:8888).

Create a New Notebook:

In the Jupyter interface, you will see an option to create a New Notebook. Select Python 3 from the dropdown menu to create a new notebook where you can write and execute Python code interactively. To write Python code, click on a cell and start typing. For example, write:

%matplotlib widget
import matplotlib.pyplot as plt
fig=plt.figure()
fig.clear();
ax=fig.add_subplot(111);
ax.set_xlabel("x");

Press Shift + Enter to run the code. The output will appear directly below the cell.

You can also use Markdown cells to write formatted text. To create a Markdown cell, click on a cell, then change its type by selecting Cell > Cell Type > Markdown from the top menu. In the Markdown cell, you can write text, create headers, bullet points, etc., which is helpful for documenting your code.