我有一些簡單的代碼可以用 Seaborn 創建一個十六進制圖。我想使用 viridis 調色板,但我希望它是白色的,密度為 0。這可能嗎?我希望下面的紫色塊是白色/不可見。
g = sns.jointplot(x =depth, y = abs(depth-med), kind="hex", joint_kws={"color":'White', 'cmap':'viridis'})
sns.set_style("whitegrid")

uj5u.com熱心網友回復:
您可以將vmin標準化設定為低于 1 的值,并將裁剪的資料點設定為白色:
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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/427096.html
標籤:Python matplotlib 阴谋 海运
