Issue
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.
Solution
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()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.