How to Create Scientific Plots Using Matplotlib, Python, and LaTeX

Berkay Kullukçu
CodeX
Published in
3 min readAug 16, 2022

This blog will show you how to create publication-ready plots for scientific journals and conferences using Matplotlib, Python, and LaTeX.

Photo by Hans Reniers on Unsplash

Python is an excellent tool for scientific plotting. The open-source nature of Python and other plotting software’s hard drive use are the main driving forces. I hope this lesson helps highlight key options and constraints so you may tailor any dataset to suit your preferences. I use Google Colaboratory for plotting; however, you can use any IDE or text editor you want. Python’s Matplotlib toolkit provides a complete tool for building static, animated, and interactive visualizations. Matplotlib makes difficult things possible and straightforward things easy.

Packages

We first need to import LaTeX to our coding environment.

If you get any errors while importing LaTeX, you may try to execute the following commands.

The package Matplotlib contains the most significant portion of the functions needed. We also import Numpy for any quick calculations and additional Pylab packages for adding zoom figures.

Loading Data

As my data is in a .csv file, I used the read_excelfunction. We can store the data in our columns in separate variables to plot. The name of the .xlsx file will be an input to the read_excel function.

Then we need to create a figure.

Photo by charlesdeluvio on Unsplash

Plotting the Data

To create our axis, we use the add_axes function. In this example, we are also implementing an inset figure.

One can change the coefficients inside the inset_axes function to find the best position for the figure.

To add labels to our plots, we use the following code.

We can also add limits to our plots based on our data.

We can change the tick frequency of our plots.

To make the fonts more prominent, we can use a for-loop to iterate over different plot components where we can change the font sizes.

We can use the following code to manipulate the inset figure frame and create a zoom effect.

In the last step, we can save the figure to the environment and display it on the console.

Here is the entire code we developed throughout the tutorial.

Conclusion

Finally! Using Python, we were able to create a plot that is suitable for publishing. Thank you for reading, and I’ll keep adding new instructions and examples. For other articles and updates, connect with me on LinkedIn. You can also follow me on Github.

--

--