Seaborn HeatMap - How To Set Colour Grading Throughout Multiple Different Datasets
So I need to create a number of heatmaps in seaborn with varying datascales. Some range from 0-100 and some +100 to -100. What I need to do is to keep the colour grading the same t
Solution 1:
To specify the color normalization, you can use a Normalize
instance, plt.Normalize(vmin, vmax)
and supply it to the heatmap using the norm
keyword (which is routed to the underlying pcolormesh
).
To obtain a colormap with gradually changing colors, you may use the static LinearSegmentedColormap.from_list
method and supply it with a list of colors.
import numpy as np; np.random.seed(0)
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
x1 = np.random.randint(0,100,size=(12,8))
x2 = np.random.randint(-100,100,size=(12,8))
fig, axes = plt.subplots(ncols=2)
cmap = mcolors.LinearSegmentedColormap.from_list("n",['#000066','#000099','#0000cc','#1a1aff','#6666ff','#b3b3ff',
'#ffff00','#ffcccc','#ff9999','#ff6666','#ff3333','#ff0000'])
norm = plt.Normalize(-100,100)
sns.heatmap(x1, ax=axes[0], cmap=cmap, norm=norm)
sns.heatmap(x2, ax=axes[1], cmap=cmap, norm=norm)
plt.show()
Post a Comment for "Seaborn HeatMap - How To Set Colour Grading Throughout Multiple Different Datasets"