NightLearner
NightLearner

Reputation: 305

how do use a Python colormap in two directions

I have some data that I am trying to plot, that goes from -1 to 0 to 1 and I would like to use a color bar such as "Viridis" but I would like to have the same colors run from -1 (yellow) to 0 (purple), and then flip directions and from from 0 (purple) to 1 (yellow). Essentially have the purple in the middle at 0 and have the exact same transitions in colors for 0 to -1 and 0 to 1. In this instance I could just have the values has aboslute values but moving forward with the code, I wont be able to do that for a variety of reasons.

any help is appreciated!

Upvotes: 0

Views: 324

Answers (1)

warped
warped

Reputation: 9482

You can extract the colors from reversed viridis into a list, and then extend this list with its reversed version:

import matplotlib 
import matplotlib.pyplot as plt
import numpy as np 

colors = [plt.cm.viridis_r(a) for a in np.linspace(0,1,100)]
colors = colors + colors[::-1]
cmap = matplotlib.colors.ListedColormap(colors)


x = range(100)

plt.scatter(x,x,c=x, cmap=cmap)

enter image description here

Upvotes: 2

Related Questions