Creating scientific graphs is crucial for data analysis and presentation in research. Python offers robust tools for generating high-quality plots with libraries like Matplotlib [1] and NumPy [2]. These tools help researchers visualize complex data, highlight specific regions, and present findings with clarity and precision.

This article explores how to plot graphs in Python. It focuses on techniques to enhance scientific visualizations, ensuring data is both understandable and impactful for scientific communication.

**How to have LaTeX font in Matplotlib?**

Matplotlib is a versatile plotting library for Python. It can generate high-quality graphs suitable for publication. A powerful feature of Matplotlib is its ability to render text using LaTeX. This is essential for creating professional-looking plots in scientific and mathematical contexts. This section demonstrates how to create a plot with LaTeX-formatted text in Matplotlib.

**Importing Required Libraries**:

To begin with, we need to import the necessary libraries. The main library is

, and we also use **matplotlib.pyplot**`numpy`

for generating data. Additionally,

from **rcParams**`matplotlib`

is imported to customize the default settings.

**Code Overview**:

**Enable LaTeX Text Rendering**:

LaTeX is enabled for all text elements in the plot. This means that any text in the plot, including axis labels and tick labels, will be rendered using LaTeX, which provides superior typesetting for mathematical expressions.**Set the Font Family**:

The font family is set to ‘serif’, giving the text a classic LaTeX look, which is preferred for academic and professional presentations.**Include the**:`amsmath`

Package

The`amsmath`

package is included in the LaTeX preamble. This package allows the use of advanced mathematical commands and environments, enhancing the typesetting capabilities.**Format Tick Labels**:

Tick labels on both the x and y axes are formatted using LaTeX math mode. The labels are displayed as integer values, ensuring they are clear and well-presented.**Label the Axes**:

The axis labels are set using LaTeX math mode. For instance, the x-axis label might be formatted as`$x \, (\text{units})$`

, and the y-axis label as`$y = x^2 - 5 \, (\text{units})$`

. This ensures that variables and units are presented correctly, enhancing readability and professionalism.

By incorporating LaTeX, the plot achieves a high standard of presentation, suitable for use in academic papers, presentations, and other professional settings.

**Highlighting specific regions in a graph**

Highlighting specific regions in a graph is very useful. It helps emphasize certain areas of interest. This is particularly important in scientific and mathematical plots. Certain ranges of data often need to be visually distinguished. Matplotlib provides simple and effective ways to highlight these regions.

**Code Overview**

**Text rendering**: Uses LaTeX for rendering text.**Font properties**: Sets LaTeX font and includes the`amsmath`

package.**Data generation**: Creates`x`

data points and computes`y`

values for the quadratic function \(y = x^2 – 5\).**Highlight regions**: Highlights three regions on the plot using`axvspan`

and labels them.**Plotting**: Plots the quadratic function and adds dashed lines for the x-axis and y-axis.**Tick parameters**: Configures the appearance of ticks.**Limits and labels**: Sets axis limits, ticks, and formats tick labels with LaTeX.**Axis labels**: Adds labels to the x and y axes.**Layout and display**: Adjusts layout and displays the plot.

**Dual y-axes plots in Python**

One notable feature of Matplotlib is the ability to create dual-axis plots. In these plots, two different y-axes share the same x-axis. This allows for the simultaneous visualization of data with different scales.

**Code Overview**:

- Two functions are defined: \( y_1 = -e^{x / 10} \) and \( y_2 = e^{x / 10} \).
- A figure and axis (
`ax1`

) are created, and the first function \(-e^{x / 10}\) is plotted with labels formatted in LaTeX. - A secondary y-axis (
`ax2`

) is created using`twinx()`

, and the second function \( e^{x / 10} \) is plotted with its labels also formatted in LaTeX. - LaTeX font properties are set to ensure all text is rendered using LaTeX.
- Legends are added to distinguish between the two functions.

**Dual x-axes plot in Python**

In scientific and engineering applications, it’s often necessary to visualize data with different scales on the same plot. Python’s Matplotlib library is a powerful tool for creating such complex plots. This section demonstrates how to plot a function with two x-axes using Matplotlib, with a practical example.

Let’s consider the function \( y = e^{-\frac{x_1}{2}} \sin(5x_1) \), which we want to plot against two different x-axes: \( x_1 \) and \( x_2 \), where \( x_2 = \frac{x_1}{10} \). The following Python code achieves this using Matplotlib:

**Code Overview**

- \( x_1 \) and \( y_1 \) are generated, and \( x_2 \) is calculated as \( x_1 / 10 \).
- The primary x-axis (
`ax1`

) displays \( x_1 \). - The secondary x-axis (
`ax2`

) is synchronized with \( x_1 \) and displays \( x_2 \) by setting its limits to match`ax1`

and adjusting the tick labels accordingly. - The plot displays \( y_1 \) against both \( x_1 \) and \( x_2 \).

**Multiple panels with shared axes**

In this section, we demonstrate how to create a figure with multiple subplots that share the same \(x\)-axis, allowing for a clear comparison of different functions plotted over the same range.

**Code Overview**:

The following functions have been plotted in the code below.

- \(y_1= e^{-0.1x} \cos(2 \pi x^2) \).
- \(y_2 = \sin(4x^2) e^{-(x-5)^2} \).
- \(y_3= 1 \times 10^7 e^{-(\sin(4x) – 5)^2} \).

**Multiple panels in a grid layout**

The following code generates a graph with four subplots in a \(2 \times 2\) grid. Each subplot shares the \(x\)-axis with the other subplots in its column and the \(y\)-axis with the other subplots in its row. It also customizes the plots using LaTeX for text rendering and sets various parameters like line width and tick settings.

**Code overview:**

- Import necessary libraries.
- Configure figure size, axes linewidth, and LaTeX text rendering.
- Generate data for four functions.
- Create a figure with four subplots.
- Plot each function in respective subplots with labels, ticks, and legends.
- Adjust layout for minimal spacing.
- Display the plot.

**Contour plots in Python**

Contour plots are an excellent way to visualize three-dimensional data in two dimensions by plotting constant z slices, known as contours, on a two-dimensional plane. The provided code demonstrates how to generate a contour plot using Matplotlib in Python.

**Code Overview:**

- Import necessary libraries (
`matplotlib`

,`numpy`

). - Configure figure size, axes linewidth, and enable LaTeX for text rendering.
- Set font properties and LaTeX preamble to include the
`amsmath`

package. - Generate
`x`

and`y`

data points and create a meshgrid. - Compute
`z`

values using a sine function for the contour plot. - Create a contour plot with specified levels and ‘viridis’ colormap.
- Label the contours inline with a specific font size.
- Customize tick parameters for both axes.
- Set axis limits and tick positions.
- Format tick labels using LaTeX.
- Label the
`x`

and`y`

axes with LaTeX-formatted labels. - Apply a tight layout and display the plot.

**Bubble Plot in Python (2D)**

A bubble plot is a type of scatter plot where each data point is represented by a bubble. The **\(x-\)axis** and** \(y-\)axis **denote two variables, while the **size of the bubble** indicates a third variable. **Colors** can also be used to represent a fourth variable. Bubble plots are useful for simultaneously visualizing relationships between multiple dimensions of data. Here is an example of a bubble plot in Python:

**Example Data Set (Not Based on Actual Records)**

The table below presents data from 2000 to 2024, detailing the number of published papers in matter wave technology, the number of researchers involved, and the citations received each year. It highlights trends in research activity and impact within matter wave technologies, aiding in visual analysis via a bubble plot. Note that the data is not based on actual records.

Year (\(x-\)axis) | Number of Published Papers (\(y-\)axis) | Number of Researchers (Size of the bubbles) | Citations (Colorbar) |
---|---|---|---|

2000 | 15 | 45 | 300 |

2001 | 20 | 60 | 400 |

2002 | 16 | 54 | 360 |

2003 | 18 | 66 | 440 |

2004 | 22 | 75 | 500 |

2005 | 27 | 81 | 540 |

2006 | 30 | 90 | 600 |

2007 | 32 | 96 | 640 |

2008 | 35 | 105 | 700 |

2009 | 34 | 111 | 740 |

2010 | 40 | 120 | 800 |

2011 | 44 | 126 | 840 |

2012 | 45 | 135 | 900 |

2013 | 48 | 144 | 960 |

2014 | 50 | 150 | 1000 |

2015 | 52 | 156 | 1040 |

2016 | 55 | 165 | 1100 |

2017 | 58 | 174 | 1160 |

2018 | 60 | 180 | 1200 |

2019 | 62 | 186 | 1240 |

2020 | 65 | 195 | 1300 |

2021 | 68 | 204 | 1360 |

2022 | 70 | 210 | 1400 |

2023 | 73 | 219 | 1460 |

2024 | 75 | 225 | 1500 |

**Code Overview:**

The code mentioned below generates a bubble plot to visualize research trends in matter wave technology from 2000 to 2024.

Key points of the code –

**X-axis**: Years of publication (2000-2024).**Y-axis**: Number of published papers per year.**Bubble size**: Number of researchers involved, scaled by 1.5.**Bubble color**: Total citations per year, using the ‘hot’ colormap.**Labels**: X-axis labeled ‘Year’ and Y-axis labeled ‘Number of Published Papers’.

**Bubble Plot in Python (3D)**

Now we have visualized research trends over time for multiple subjects using a 3D bubble plot. It plots published papers, researchers, and years, with bubble sizes representing normalized citations. Different subjects are color-coded, and LaTeX is used for text rendering. Axis labels, tick parameters, and a legend are customized for clarity, and the plot is displayed.

**Code Overview:**

- Import necessary libraries (
`matplotlib`

,`numpy`

). - Configure figure size, axes linewidth, and enable LaTeX for text rendering.
- Define data for multiple subjects: years, published papers, researchers, citations, and impact factors.
- Normalize citations for bubble size visualization.
- Create a 3D scatter plot with color-coded subjects.
- Use a dictionary to assign colors to each subject.
- Plot data points for each subject with bubble sizes representing normalized citations.
- Set axis labels and customize tick parameters using LaTeX formatting.
- Configure FixedLocator and FixedFormatter for tick labels.
- Add a legend and adjust the layout.
- Display the plot.

**Surface plot in Python**

Matplotlib allows for the creation of 3D surface plots, which visualize three-dimensional data effectively. The example code sets up the figure size, enables LaTeX rendering for text, and generates data using `numpy`

. A 3D surface plot is created with the ‘plasma’ colormap, enhancing visual appeal.

**Code Overview**

- Import necessary libraries (
`matplotlib`

,`numpy`

). - Configure figure size and axes linewidth.
- Enable LaTeX for text rendering and set font properties.
- Set LaTeX preamble to include
`amsmath`

package. - Generate
`x`

and`y`

data points and create a meshgrid. - Compute
`z`

values using a sine function. - Create a 3D plot with a surface plot using ‘plasma’ colormap.
- Customize ticks and labels for all axes.
- Set axis limits and tick positions.
- Format tick labels using LaTeX.
- Label axes with LaTeX-formatted labels.
- Apply tight layout and display the plot.

**Visualizing electric field lines using Streamplot in Python**

In this section, we’ll explore how to visualize the electric fields created by a system of point charges using Python. We will consider three positive charges arranged in an equilateral triangle and a single negative charge at the centroid (center of mass) of the triangle. The electric field is calculated using Coulomb’s law, and the resulting field lines are plotted to provide a clear visual representation. Here is the mathematical steps to calculate electric fields:

**Positions of Charges**:

- Three positive charges, \( +q \), are placed at the vertices of an equilateral triangle.
- One negative charge, \( -q \), is placed at the centroid of the triangle.

**Electric Field Calculation**:

- The electric field \( \mathbf{E} \) due to a point charge \( q \) at a position \( \mathbf{r} \) is given by Coulomb’s law:

\[\mathbf{E} = \frac{kq}{r^2} \hat{r}\]

where \( k \) is Coulomb’s constant, \( r \) is the distance from the charge to the point where the field is being calculated, and \( \hat{r} \) is the unit vector pointing from the charge to that point.

**Electric Field Components**:

- The components of the electric field \( \mathbf{E}\) at a point (\(x, y\)) due to a charge \( q \) at position (\(x_0, y_0\)) are:

\[E_x = k \frac{q (x – x_0)}{r^3}\]

\[E_y = k \frac{q (y – y_0)}{r^3}\]

where \( r = \sqrt{(x – x_0)^2 + (y – y_0)^2} \).

**Superposition Principle**:

- The total electric field at any point is the vector sum of the electric fields due to all individual charges. The code provided below initializes the electric field components \( E_x \) and \( E_y \) to zero and then iteratively adds the contributions from each charge.

**Code Overview:**

- Import necessary libraries (
`numpy`

,`matplotlib`

). - Configure figure size, axes linewidth, and LaTeX text rendering.
- Define constants: Coulomb’s constant (
`k`

), charge (`q`

). - Define positions of charges and calculate the center of mass.
- Create a grid of points for the electric field calculation.
- Define a function to calculate electric field components.
- Initialize electric field components to zero.
- Calculate and sum electric field contributions from each charge.
- Calculate and add the electric field contribution from a negative charge at the center of mass.
- Plot streamlines of the electric field.
- Plot charge positions and customize tick labels using LaTeX.
- Add axis labels, a legend, and display the plot.

## References

[1] Matplotlib, https://matplotlib.org/

[2] NumPy, https://numpy.org/