1

I am not able to find in matplotlib a colormap that is similar to "Green-White-Red" in Excel's conditional formatting. "RdYlGn" is farily close, but the middle values are yellow, instead of white. The custom scale here seems off as well.

How do I find or create one similar to that in Excel? Thanks.

Reference:

https://matplotlib.org/stable/tutorials/colors/colormaps.html

iwbabn
  • 1,275
  • 4
  • 17
  • 32

1 Answers1

0

I took a function for visualising colour-maps from https://matplotlib.org/stable/tutorials/colors/colormaps.html, and an answer from here to visualise different variations of the colour-map:

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


gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(cmap_list):
    # Create figure and adjust figure height to number of colormaps
    nrows = len(cmap_list)
    figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
    fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
    fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
                        left=0.2, right=0.99)

    for ax, name in zip(axs, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
                transform=ax.transAxes)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axs:
        ax.set_axis_off()

    # Save colormap list for later.


c = ["darkred", "red", "lightcoral", "white", "palegreen", "green", "darkgreen"]

cmaps = []

v = [0, .15, .4, .5, .6, .9, 1.]        # change these
l = list(zip(v, c))
cmaps.append(LinearSegmentedColormap.from_list('rg', l))
v = [0, .15, .25, .5, .75, .9, 1.]
l = list(zip(v, c))
cmaps.append(LinearSegmentedColormap.from_list('rg', l))
v = [0, .1, .2, .5, .8, .9, 1.]
l = list(zip(v, c))
cmaps.append(LinearSegmentedColormap.from_list('rg', l))

plot_color_gradients(cmaps)

plt.show()

Change v for slightly different values to make exactly what you are looking for.

Rik Mulder
  • 245
  • 1
  • 9