INNER CODE UNIT · Python
draw_cmap
stefmolin/Hands-On-Data-Analysis-with-Pandas-2nd-edition · ch_06/color_utils.py:94
def draw_cmap(cmap, values=np.array([[0, 1]]), **kwargs):
"""
Draw a colorbar for visualizing a colormap.
Parameters:
- cmap: A matplotlib colormap
- values: The values to use for the colormap, defaults to [0, 1]
- kwargs: Keyword arguments to pass to `plt.colorbar()`
Returns:
A matplotlib `Colorbar` object, which you can save with:
`plt.savefig(<file_name>, bbox_inches='tight')`
"""
img = plt.imshow(values, cmap=cmap)
cbar = plt.colorbar(**kwargs)
img.axes.remove()
return cbar