0

I'm plotting a simple heatmap with a skewed distribution of values

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

size=100
data=np.array([[random.expovariate(1) for _ in range(size)] for _ in range(size)])
fig, ax=plt.subplots()
heatmap=ax.pcolormesh(data, cmap=matplotlib.cm.Reds)
fig.colorbar(heatmap)

It would be great, if I could change the color scaling such that values below some threshold are a fixed color (for example lowest color in the cmap) and all other values are scaled to show a more uniform distribution of colors (for example exponential or power rescaling with some parameter).

Is there an easy way to rescale my colormap without changing my data values?

Gere
  • 12,075
  • 18
  • 62
  • 94

1 Answers1

2

If you are happy with a linear bit of the scale, there is:

heatmap=ax.pcolormesh(data, cmap=matplotlib.cm.Reds, vmin=0, vmin=1)

Now the colors are scaled form 0 to 1.

If you want to have a non-linear colormap, it is possible, as well. In order to get it and the respective color bar correct, you'll need to jump through some hoops.

The accepted answer to nonlinear colormap, matplotlib should give you the recipe.

Community
  • 1
  • 1
DrV
  • 22,637
  • 7
  • 60
  • 72