1

I saw JohanC's answer here:

How to set fixed color ranges with Seaborn heatmap?

When I made my plot, I had two types of cells that I needed to highlight: those with fixed values (e.g.,0 and 1), and those that varied within a range (e.g., 9-200). For the first type, fixed colors look nice. For the second type, a standard colorbar looks nice. Is there a way to have both, i.e., specify a set of cells to conditionally use fixed colors and if they go above a certain value, use a colorbar to color them?

Thanks, Michael

Regular colorbar example

sns.heatmap(np.clip(np.random.rand(21, 12) - 0.1, 0, 1)*100)
plt.show()

1 Answer 1

1

The most straightforward might be to use the mask in heatmap to plot the two cmaps independently:

from matplotlib.colors import BoundaryNorm, ListedColormap
import seaborn as sns

# example
np.random.seed(0)
data = np.clip(np.random.rand(21, 12)*1.2 - 0.1, 0, 1)*100

# identify values to mask (here 0 and 100)
mask = (data == 0) | (data == 100)

# plot fixed values
cmap = ListedColormap(['green', 'blue'])
norm = BoundaryNorm([0, 50, 100], len(cmap.colors))
ax = sns.heatmap(data, mask=~mask, cmap=cmap, norm=norm)

# plot the rest
sns.heatmap(data, mask=mask, ax=ax)

Output:

seaborn heatmap fixed + continuous cmap

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

Comments

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.