'Dark2'] matplotlib.colormaps[
plot_cmap_collections
plot_cmap_collections (cmap_collections:str|list[str]=None)
Plot all color maps in the collections passed as cmap_collections
Type | Default | Details | |
---|---|---|---|
cmap_collections | str | list[str] | None | list of color map collections to display (from cmaps.keys()) |
The following color map collections are defined:
- Cyclic
- Diverging
- Miscellaneous
- Perceptually Uniform Sequential
- Qualitative
- Sequential
- Sequential (2)
plot_cmap_collections
will plot a color bar for each color map in the selected collections:
- A single collection
'Cyclic') plot_cmap_collections(
- Several collections
'Qualitative', 'Sequential']) plot_cmap_collections([
- All the collections
plot_cmap_collections()
plot_color_bar
plot_color_bar (cmap:str, series:list[typing.Union[int,float]]=None)
Plot a color bar with value overlay from series
based on cmap
Type | Default | Details | |
---|---|---|---|
cmap | str | string name of one of the cmaps | |
series | list | None | series of numerical values to show for each color |
'tab10', range(10)) plot_color_bar(
'tab10', series=range(6)) plot_color_bar(
'tab10', series=[0, 1, 2]) plot_color_bar(
get_color_mapper
get_color_mapper (series:list[typing.Union[int,float]], cmap:str='tab10')
Return color mapper based on a color map and a series of values
Type | Default | Details | |
---|---|---|---|
series | list | series of values to map to colors | |
cmap | str | tab10 | name of the cmap to use |
Usage
This function is used to ensure coherent colors for different plots.
- Define a color mapper based on values and cmap:
clr_mapper = get_color_mapper([1, 2, 3, 4], cmap='Paired)
- Call the color mapper and have it return the appropriate values for any plot:
clr_mapper.to_rgba(2)
Example
Let’s take a dataset \((X,y)\), generated by sklearn using make_blob
for 5,000 samples with 6 features each and grouped in 10 centers. The \(X\) and \(y\) are saved as npy
file
import pandas as pd
with open('data-dev/ten-blobs-6-cols-X.npy', 'rb') as fp:
= np.load(fp)
X with open('data-dev/ten-blobs-6-cols-y.npy', 'rb') as fp:
= np.load(fp)
y
= X.shape[1]
n_feats = [f"col_{i}" for i in range(n_feats)]
col_list
= pd.DataFrame(X, columns=col_list)
X 3) X.head(
col_0 | col_1 | col_2 | col_3 | col_4 | col_5 | |
---|---|---|---|---|---|---|
0 | 8.714921 | 0.580067 | 6.817529 | -4.891837 | -0.418419 | -4.127050 |
1 | -0.322263 | -3.901781 | -8.588875 | 1.114151 | 4.908420 | -2.475176 |
2 | -7.798702 | -4.276232 | -1.145314 | -6.619232 | -9.232606 | 6.994045 |
1. Define a color mapper based on values and cmap
We have clustered the 5,000 sample of \(X\) into 10 clusters (saved into a clusters.npy
file)
Now we want to make a scatter plot of two of the features, displaying the 10 cluster using a cmap
.
To ensure that we can keep the same cluster color mapping for other plots, we use clr_mapper
to predefine how colors are mapped to each cluster:
clr_mapper = get_color_mapper(cluster_ids, cmap=cmap)
.
# Load clustered data from the blobs
with open('data-dev/ten-blobs-6-cols-clusters.npy', 'rb') as fp:
= np.load(fp)
clusters
= np.unique(clusters)
cluster_ids cluster_ids
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
='tab10'
cmap
= plt.subplots(nrows=1, ncols=2, figsize=(14, 3))
fig, (ax1, ax2)
= ax1.scatter(X.col_0, X.col_1, c=clusters, s=2, cmap=cmap)
im1 '2 first features, colored by cluster value')
ax1.set_title(
fig.colorbar(im1)
= ax2.scatter(X.col_4, X.col_5, c=clusters, s=2, cmap=cmap)
im2 '2 last features, colored by cluster value')
ax2.set_title(
fig.colorbar(im2) plt.show()
= get_color_mapper(cluster_ids, cmap=cmap) clr_mapper
Call the color mapper and use it in any plot
Use for another plot, showing another feature, and its value for each sample, colored according to its cluster
= 'col_4'
featname =(12, 3))
plt.figure(figsize='grey', alpha=.66, lw=0.25)
plt.plot(X[featname], cf'{featname}.')
plt.title(
plt.show()
=(12, 3))
plt.figure(figsize='grey', alpha=.5, lw=0.1)
plt.plot(X[featname], cfor c in cluster_ids:
= y == c
mask f"{featname}_{c}"] = X.loc[:, featname]
X[~mask, f"{featname}_{c}"] = np.nan
X.loc[f"{featname}_{c}"], label=str(c), c=clr_mapper.to_rgba(c), lw=0, marker='o', markersize=1)
plt.plot(X[f'{featname}. Data points colored according to the cluster it belongs to.')
plt.title(
plt.legend() plt.show()