Scatter Artist#

In this example notebook, we show how to create a scatter plot using the Scatter class. The Scatter class is a subclass of the Artist class.

It has a simplified interface for creating scatter plots and updating some of its properties, like assigning different classes to points and displaying them with different colors based on a categorical colormap.

It can be imported like shown below:

import numpy as np
import matplotlib.pyplot as plt
from biaplotter.artists import Scatter

Creating a Scatter Plot#

To create an empty scatter plot, just instanciate the Scatter class and provide an axes object as an argument.

fig, ax = plt.subplots()
scatter = Scatter(ax)
../_images/55ad6f09974659058ed6ec303b68ef045c7c70849e2cd3bc6219f19b28ba7860.png

Adding Data to the Scatter Artist#

To add data to the scatter plot, just feed the property data with a (N, 2) shaped numpy array. The plot gets updated automatically every time one of its properties is changed.

n_samples = 100
data = np.random.rand(n_samples, 2)
scatter.data = data
fig # show the updated figure
../_images/14016d533e9022aac0fc446142d025a4587a827f019a02faf1bef0087c879fba.png

Assigning Classes to Data Points#

The Scatter artist comes with a custom categorical colormap, which can be used to assign different classes to points.

You can access the scatter current categorical colormap via its private _colormap attribute.

scatter.categorical_colormap
cat10_modified
cat10_modified colormap
under
bad
over

To assign classes to points, just feed the property color_indices with a (N,) shaped numpy array containing integers. These integers will be used as indices to the colormap.

Below, we randomly color the points with the 5 first colors of the scatter default colormap (light gray is the default color with color index 0).

color_indices = np.linspace(start=0, stop=5, num=n_samples, endpoint=False, dtype=int)
scatter.color_indices = color_indices
fig
../_images/82b98bbce0aa4c38111fae587e67923e7f2659d02343658166605931964b05f8.png

Changing the data - i.e., adding new data of the same size will keep the point properties (e.g., the color_indices) as they are:

data = np.random.rand(n_samples, 2)
scatter.data = data
fig
../_images/3069274e91b876ab7d7d48b89f498870e76fcff478982c12f4555fdaf0b99680.png

Adding new data of a different size resets the coloring:

# Adding 400 more samples
n_samples = 400
data = np.random.rand(n_samples, 2)
scatter.data = data
fig
../_images/33e06efc13b40ffd232125793d567623cdb619b3175664a2a383b1a2a2b59a22.png

Setting point properties#

You can also set other properties of the points, like their transparency. This is mediated through the alpha property. In this case, we set the transparency of the points according to the respective point’s x coordinate, so we should observe a transparency gradient from left to right.

alpha = data[:, 0]
alpha = (alpha - alpha.min()) / (alpha.max() - alpha.min())
scatter.alpha = alpha
fig
../_images/ee9301cd808e319185b45942a21d9b2198b4a7a4214b0d36e7b79cd4b1e28238.png

Again, changing the data will keep the point properties as they are:

data = np.random.rand(n_samples, 2)
scatter.data = data
fig
../_images/cd2b783a35587e706c43c4e5cb392e2a446ef704cb86aa94d16964e16dbf6a22.png

You can undo this by simply setting all alpha values to 1:

scatter.alpha = 1
fig
../_images/d1982da19775837a2a27c4c276426d6955c03eea29a15f6aab0dcefcad4e576a.png

You can also set the sizes of the points (values around ~50 are typically a good starting point):

size = data[:, 0] * 100
scatter.size = size
fig
../_images/1b21e8aaec41f8865d60b202c339fcb0f5cbaa603fa7fa1948fc510c81180679.png

And again, changing the data but keeping the size of the data (i.e., the number of samples, to be more precise) will keep the point properties as they are:

data = np.random.rand(n_samples, 2)
scatter.data = data
fig
../_images/91ec21c87abf9d6f1fc03e6c7d9b0f74186fd2d13d94de4dc767a80abfc41097.png

Scatter Visibility#

Optionally, hide/show the artist by setting the visible attribute.

scatter.visible = False
fig
../_images/ad2047ea833089572698e866df9230b71cf80dabc1bee09a408d3f618f098f91.png
scatter.visible = True
fig
../_images/91ec21c87abf9d6f1fc03e6c7d9b0f74186fd2d13d94de4dc767a80abfc41097.png