Python template for scientific scatter plot

Creating scatter plot for scientific paper and presentation is always pain.

Here, the template code was provided using pyplot module from the Matplotlib library. This module allows you to draw not only graphs of ordinary polynomials and exponential functions, but also scatter plots and histograms. You can also add a legend, axis labels, and change the color of the lines.

The pyplot module is commonly imported with the name plt as follows

import matplotlib.pyplot as plt

Let’s try to write a scatter plot using Numpy and Matplotlib. The outline of code below is as follows.

  • Import the necessary libraries.
  • Generate random numbers according to the standard normal distribution and store them in x and y, respectively.
  • Call template function to draw scatter plot


import numpy
import matplotlib.pyplot as plt

#------------------------------------------------------------
#- Template function to generate scientific scatter plot

def customScatter(x,y,titleP='Title',titleX='X-axis',titleY='Y-axis'):

    fig, ax = plt.subplots(figsize=(8, 8))
    plt.ticklabel_format(useOffset = False)
    plt.plot(x, y,'ob')

    # Set the title etc.
    ax.set_title(titleP,fontsize=24,pad=15)
    ax.set_xlabel(titleX,fontsize=24)
    ax.set_ylabel(titleY,fontsize=24)

    # Display Grid.
    ax.grid(True,linestyle='-',color='0.85')
    # Set boundary of plot.
    for label in ax.xaxis.get_ticklabels():label.set_fontsize(22)
    for label in ax.yaxis.get_ticklabels():label.set_fontsize(22)
    # Remove ticks
    ax.tick_params(which='major',length=0)
    ax.tick_params(which='minor',length=0)



#----------------------------------------------------
#- Test

x = numpy.random.default_rng().uniform(-10,10,50)
y = numpy.random.default_rng().uniform(-10,10,50)

customScatter(x,y)

Python template for scientific scatter plot