0

Is there a way to change the transition values of a continuous colormap (cmap) in matplotlib? I want to use "vlag" to color a heatmap, however my values only typically range from 0 to 0.6 (instead of 0-1). I could renormalize my data or use vmin & vmax, however I was curious if there was a way to set transition points for vlag between 0-1. There are three colors in vlag (blue, white, and red). Having set transition points will allow for an apples to apples comparison between different heatmaps.

2
  • Hi @JohanC, BoundaryNorm creates categorical colors from continuous data. I want to retain the continuous color just change the transition points. Thank you for the suggestion! Commented Jan 7, 2022 at 20:43
  • 2
    Please see matplotlib.org/stable/tutorials/colors/… Commented Jan 7, 2022 at 21:17

1 Answer 1

2

If the colormap only contains few colors, a BoundaryNorm lets you specify the transition points.

For a colormap with a smooth range of colors, a TwoSlopeNorm lets you move the spots where the transitions start happening.

from matplotlib.colors import TwoSlopeNorm
import seaborn as sns  # for the 'vlag' colormap
import numpy as np

x = np.linspace(0, 10, 200)
y = np.sin(x)**2

fig, axs = plt.subplots(ncols=2, figsize=(12, 4))
scat0 = axs[0].scatter(x, y, c=y, cmap='vlag')
axs[0].set_title('default norm')
plt.colorbar(scat0, ax=axs[0])

norm = TwoSlopeNorm(vmin=0., vcenter=0.3, vmax=1)
scat1 = axs[1].scatter(x, y, c=y, cmap='vlag', norm=norm)
axs[1].set_title('TwoSlopeNorm')
plt.colorbar(scat1, ax=axs[1])

plt.tight_layout()
plt.show()

example of TwoSlopeNorm

Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful! Thank you @JohanC!, TwoSlopeNorm is exactly what I was looking for, also the comment by Jody was also very helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.