Home > OS >  Edit colorbar by increasing one of the color range
Edit colorbar by increasing one of the color range

Time:10-04

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)

plt.subplot(111)
plt.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r)

plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
cax = plt.axes([0.85, 0.1, 0.075, 0.8])
plt.colorbar(cax=cax)
plt.show()

In the above example, how do I edit the colormap to increase the violet color? Something like biased to violet.

CodePudding user response:

Here are two examples to change the colormap. At the left, the orinal version is shown. In the center, the 60% of the lower colors is used. At the right, the colors near purple are sampled more, and the colors at the top less.

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np

np.random.seed(19680801)
data = np.random.random((100, 100))

fig, axs = plt.subplots(ncols=3, figsize=(15, 5))

for ax in axs.flat:
    if ax == axs[0]:
        cmap = plt.cm.BuPu_r
        ax.set_title('"BuPu_r" color map')
    elif ax == axs[1]:
        cmap = ListedColormap(plt.cm.BuPu_r(np.linspace(0, 0.6, 256)))
        ax.set_title('60% lower subset of the color map')
    else:
        cmap = ListedColormap(plt.cm.BuPu_r(np.linspace(0, 1, 256) ** 2))
        ax.set_title('expanding lower part and\ncompressing upper part')
    img = ax.imshow(data, cmap=cmap)
    plt.colorbar(img, shrink=0.8, ax=ax)
plt.tight_layout()
plt.show()

comparing cmaps

  • Related