ribs.visualize.grid_archive_heatmap¶
- ribs.visualize.grid_archive_heatmap(archive, ax=None, *, transpose_measures=False, cmap='magma', aspect=None, vmin=None, vmax=None, cbar='auto', pcm_kwargs=None, cbar_kwargs=None)[source]¶
Plots heatmap of a
GridArchive
with 1D or 2D measure space.This function creates a grid of cells and shades each cell with a color corresponding to the objective value of that cell’s elite. This function uses
pcolormesh()
to generate the grid. For further customization, pass extra kwargs topcolormesh()
through thepcm_kwargs
parameter. For instance, to create black boundaries of width 0.1, pass inpcm_kwargs={"edgecolor": "black", "linewidth": 0.1}
.Examples
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> from ribs.archives import GridArchive >>> from ribs.visualize import grid_archive_heatmap >>> # Populate the archive with the negative sphere function. >>> archive = GridArchive(solution_dim=2, ... dims=[20, 20], ... 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)) >>> grid_archive_heatmap(archive) >>> plt.title("Negative sphere function") >>> plt.xlabel("x coords") >>> plt.ylabel("y coords") >>> plt.show()
- Parameters
archive (GridArchive) – A 2D
GridArchive
.ax (matplotlib.axes.Axes) – Axes on which to plot the heatmap. If
None
, the current axis will be used.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'
for 2D and0.5
for 1D.'equal'
is the same asaspect=1
.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.pcm_kwargs (dict) – Additional kwargs to pass to
pcolormesh()
.cbar_kwargs (dict) – Additional kwargs to pass to
colorbar()
.
- Raises
ValueError – The archive’s dimension must be 1D or 2D.