I have some simple to code create a hex plot with Seaborn. I want to use the viridis color palette but I want it to be white where the density is 0. Is this possible? I would like the block of purple below to be white/not visible.
g = sns.jointplot(x =depth, y = abs(depth-med), kind="hex", joint_kws={"color":'White', 'cmap':'viridis'})
sns.set_style("whitegrid")
CodePudding user response:
You can set vmin
for the normalization to a value below 1 and set the clipped data points to white:
import matplotlib.pyplot as plt
from matplotlib import cm
import seaborn as sns
my_viridis = cm.get_cmap("viridis", 1024).with_extremes(under="white")
penguins = sns.load_dataset("penguins")
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="hex", joint_kws={"color":'White', "camp": my_viridis, "vmin": 0.1})
plt.show()
Alternatively, we can change the specific value for zero of the colormap:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm, colors
import seaborn as sns
dummy = cm.get_cmap("viridis")
dummy2 = dummy(np.linspace(0, 1, 1024))
dummy2[0] = np.asarray([1, 1, 1, 1])
#or you can set zero to transparent with
#dummy2[0] = np.asarray([1, 1, 1, 0])
my_viridis = colors.ListedColormap(dummy2)
penguins = sns.load_dataset("penguins")
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="hex", joint_kws={"color":'White', "cmap": my_viridis})
plt.show()