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.
-
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!Cody Glickman– Cody Glickman2022-01-07 20:43:06 +00:00Commented Jan 7, 2022 at 20:43
-
2Please see matplotlib.org/stable/tutorials/colors/…Jody Klymak– Jody Klymak2022-01-07 21:17:19 +00:00Commented Jan 7, 2022 at 21:17
Add a comment
|
1 Answer
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()
1 Comment
Cody Glickman
Beautiful! Thank you @JohanC!, TwoSlopeNorm is exactly what I was looking for, also the comment by Jody was also very helpful!
