ribs.visualize.cvt_archive_heatmap¶
- ribs.visualize.cvt_archive_heatmap(archive, ax=None, *, plot_centroids=True, plot_samples=False, transpose_measures=False, cmap='magma', aspect='auto', ms=1, lw=0.5, vmin=None, vmax=None, cbar='auto', cbar_kwargs=None)[source]¶
Plots heatmap of a
CVTArchive
with 2D measure space.Essentially, we create a Voronoi diagram and shade in each cell with a color corresponding to the objective value of that cell’s elite.
Depending on how many cells are in the archive,
ms
andlw
may need to be tuned. If there are too many cells, the Voronoi diagram and centroid markers will make the entire image appear black. In that case, try turning off the centroids withplot_centroids=False
or even removing the lines completely withlw=0
.Examples
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> from ribs.archives import CVTArchive >>> from ribs.visualize import cvt_archive_heatmap >>> # Populate the archive with the negative sphere function. >>> archive = CVTArchive(solution_dim=2, ... cells=100, ranges=[(-1, 1), (-1, 1)]) >>> x = y = np.linspace(-1, 1, 100) >>> xxs, yys = np.meshgrid(x, y) >>> xxs, yys = xxs.flatten(), yys.flatten() >>> archive.add(solution_batch=np.stack((xxs, yys), axis=1), ... objective_batch=-(xxs**2 + yys**2), ... measures_batch=np.stack((xxs, yys), axis=1)) >>> # Plot a heatmap of the archive. >>> plt.figure(figsize=(8, 6)) >>> cvt_archive_heatmap(archive) >>> plt.title("Negative sphere function") >>> plt.xlabel("x coords") >>> plt.ylabel("y coords") >>> plt.show()
- Parameters
archive (CVTArchive) – A 2D
CVTArchive
.ax (matplotlib.axes.Axes) – Axes on which to plot the heatmap. If
None
, the current axis will be used.plot_centroids (bool) – Whether to plot the cluster centroids.
plot_samples (bool) – Whether to plot the samples used when generating the clusters.
transpose_measures (bool) – By default, the first measure in the archive will appear along the x-axis, and the second will be along the y-axis. To switch this behavior (i.e. to transpose the axes), set this to
True
.cmap (str, list, matplotlib.colors.Colormap) – The colormap to use when plotting intensity. Either the name of a
Colormap
, a list of RGB or RGBA colors (i.e. an \(N \times 3\) or \(N \times 4\) array), or aColormap
object.aspect ('auto', 'equal', float) – The aspect ratio of the heatmap (i.e. height/width). Defaults to
'auto'
.'equal'
is the same asaspect=1
.ms (float) – Marker size for both centroids and samples.
lw (float) – Line width when plotting the voronoi diagram.
vmin (float) – Minimum objective value to use in the plot. If
None
, the minimum objective value in the archive is used.vmax (float) – Maximum objective value to use in the plot. If
None
, the maximum objective value in the archive is used.cbar ('auto', None, matplotlib.axes.Axes) – By default, this is set to
'auto'
which displays the colorbar on the archive’s currentAxes
. IfNone
, then colorbar is not displayed. If this is anAxes
, displays the colorbar on the specified Axes.cbar_kwargs (dict) – Additional kwargs to pass to
colorbar()
.
- Raises
ValueError – The archive is not 2D.