Scattergl and Scatter in Python – A Simple Introduction

Hey fellow Python coder! I hope you have worked with Scatter plots before (if not have a look at the tutorial below), and in this tutorial, we will learn about two ways of implementation of scatter plots. We will make use of the scattergl and scatter libraries in this tutorial and learn how to implement visualization using both libraries.

Also Read: Matplotlib scatter plot in Python

Introduction to ScatterGL v/s Scatter

Scatter plot is the basic and very common visualization where we plot data points based on the x and y coordinate system. This works very efficiently for smaller datasets and can provide the necessary information as well about the dataset.

Features of Scatter Plot:

  1. They are very simple to create, read and understand.
  2. They are suitable for small to medium-sized datasets.
  3. They come along with basic customization options like colors, markers, sizes, and more.

ScatterGL is a modern approach towards implementing scatter plots which comes with a lot of variations by making use of Web Graphics Libraries.  This approach even works efficiently for larger datasets allowing faster and smoother performance.

Features of ScatterGL Plot:

  1. They are well designed for large datasets even with millions of points.
  2. They also come along with interactive features like zooming and hovering over data points.
  3. They do have fewer customization options but that’s due to the performance factor.

Code Implementation of Scatter in Python

We will start by plotting a simple plot with let’s say only 10 data points using the code below. We will generate random x and y coordinate points and then simply plot them using the scatter function.

import numpy as np
import matplotlib.pyplot as plt


x = np.random.randn(10)
y = np.random.randn(10)
plt.scatter(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot using Scatter')
plt.show()

The resulting plot displayed on the screen is as follows.

Scatter plot in Python

In the next step, we will use the same code snippet but change the number of data points getting generated. In this case, let’s assume we will plot 100000 points and the rest of the plot remains the same. Have a look.

x = np.random.randn(100000)
y = np.random.randn(100000)
plt.scatter(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot using Scatter')
plt.show()

The resulting plot displayed on the screen is as follows.

Complex Scatter Plot

You can observe, that we can get close to no information through this visualization as the dataset is quite large. This is when scatterGL comes to our rescue in the next section.

Code Implementation of ScatterGL in Python

We will plot the same two visualizations using the scatterGL library and observe the difference in both visualizations. Have a look at the code snippet below which will visualize a simple plot with 10 data points. For the visualization, we will make use of the plotly library as shown below.

import plotly.express as px
x = np.random.randn(10)
y = np.random.randn(10)
fig = px.scatter(x=x, y=y, title='Scatter Plot using ScatterGL')
fig.show()

The resulting plot displayed on the screen is as follows.

Now the major difference can be observed when we encounter a large dataset (let’s say 100000 points) as implemented in the code snippet below.

x = np.random.randn(100000)
y = np.random.randn(100000)
fig = px.scatter(x=x, y=y, title='Scatter Plot using ScatterGL')
fig.show()

The resulting plot displayed on the screen is as follows.

You can observe that the visualization is quite interactive as compared to the static plot we visualized earlier.

Also Read:

  1. Scatter plot using ggplot2 in Python with customization
  2. Simple Random Art Generation using Python Programming
  3. How to Plot Line of Best Fit in Python

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *