Generating a Scatter Plot in Python is probably one of the most common plots, and it can easily be accomplished. This tutorial shows using how using Seaborn, we can accomplish it.
First and foremost, the function below has some dependencies around seaborn and matplotlib, so please make sure you install them.
Generating the Scatter Plot
Secondly, this post plots an example of a bacterial exponential growth where the x-axis is the “Time (Hours)” and the y-axis is the bacterial growth as the number of bacterial cells.
Please don’t assume that the growth function is real – this is just an example.
# !/usr/bin/env python3 # -*- coding: utf-8 -*- import seaborn import numpy as np import matplotlib.pyplot as matplotlib from matplotlib.lines import Line2D def simple_scatter_plot(x_data, y_data, output_filename, title_name, x_axis_label, y_axis_label): """Simple scatter plot. Args: x_data (list): List with x-axis data. y_data (list): List with y-axis data. output_filename (str): Path to output image in PNG format. title_name (int): Plot title. x_axis_label (str): X-axis Label. y_axis_label (str): Y-axis Label. """ seaborn.set(color_codes=True) matplotlib.figure(1, figsize=(9, 6)) matplotlib.title(title_name) ax = seaborn.scatterplot(x=x_data, y=y_data) ax.set(xlabel=x_axis_label, ylabel=y_axis_label) matplotlib.savefig(output_filename, bbox_inches='tight', dpi=300) matplotlib.close()
The simple_scatter_plot function requires for a few parameters described below:
- x_data: List with x-axis data
- y_data: List with y-axis data
- output_filename: Path to output image in PNG format
- title_name: Plot title
- x_axis_label: X-axis Label
- y_axis_label: Y-axis Label
Last by not least, execute the function by adapting it to the inputs you need!
>>> import simple_scatter_plot >>> >>> time_hours = range(100) >>> number_bacteria_hours = [xx ** 2 for xx in time_hours] >>> simple_scatter_plot(time_hours, number_bacteria_hours, "my_scatter_plot.png", "Bacterial Exponential Function Example", "Time (Hours)", "# Bacteria")
Here is how the Scatter Plot in Python should look like:
In short, this tutorial is just a simple recipe that can be adapted into a more complex scatter plot. Here are the seaborn docs on all the different parameters that scatter plot can take. Furthermore, please feel free to ask questions below – I’m more than happy to add your requests to this tutorial.
log log Plot Python
# please add to the function simple_scatter_plot # or to any code you have written with seaborn/matplotlib # y-axis in log scale ax.set(yscale="log") # x-axis in log scale ax.set(xscale="log")
Here are two of my favorite Data Visualization Python Books in case you want to learn more about it.
- Mastering Python Data Visualization by Kirthi Raman
- Python Data Visualization: An Easy Introduction to Data Visualization in Python with Matplotlip, Pandas, and Seaborn by Samuel Burns
In summary, this tutorial showed you how to use seaborn to plot a simple scatter plot which can be easily expanded to a more complex version.
Moreover, let me know if you need help expanding it to multiple layers. I’m more than happy to help with it. Please leave a comment below if needed.