'How to specify color of extension triangle when using ListedColormap

This answer provides an example of how to use ListedColormap to create a colormap.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap

tab20c = cm.get_cmap('tab20c', 256)
newcolors = tab20c(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors)

I know I could just use the tab20c color map and not ListedColormap in this particular case shown here, but the more complicated example discussed in that answer requires ListedColormap. I avoid that complication because it is not the focus of this question. Use newcmp to plot a function:

mock=\
np.array([[0.1,0.2,0.3],
          [0.2,0.3,0.4],
          [0.3,0.4,0.5],
          [0.4,0.5,0.6],
          [0.5,0.6,0.7],
          [0.6,0.7,0.8],
          [0.7,0.8,0.9]])
         
plt.figure(figsize=(6,6))
im = plt.imshow(mock,cmap = newcmp, vmin=0.0, vmax=1)
plt.colorbar(im,extend='min')

enter image description here

I could use the extend='min' kwarg in the colorbar call to extend the colorbar downwards.

I would like to modify the color of this triangle at the bottom of color bar. To make it, let's say, yellow, I tried the above code with tab20c = cm.get_cmap('tab20c', 256).with_extremes(under='yellow'), but the triangle remains blue. I have read about .with_extremes() here.


Question

Without changing the rest, how can set the color of the triangle below the colorbar?


I hope there is a solution involving these 3 lines:

tab20c = cm.get_cmap('tab20c', 256)
newcolors = tab20c(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors)

as they are important in customizing the colorbar for more complicated cases then presented here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source