Seaborn – 使用调色板为箱线图着色
为您的数据可视化添加正确的颜色集使其更加令人印象深刻和可读, seaborn调色板使您可以轻松地在您的可视化中使用颜色。在本文中,我们将看到如何使用seaborn调色板为箱线图着色,还将了解seaborn调色板的用途,并且它也可以应用于其他绘图。
循序渐进的方法:
步骤 1:加载为boxplot着色所需的Python包和库。
Python3
# import libraries
import seaborn as sns
import matplotlib.pyplot as plt
Python3
# loading dataset
ds = sns.load_dataset('iris')
Python3
# create boxplot object
ax = sns.boxplot(data=tips, orient="h")
Python3
# use palette method
ax = sns.boxplot(data=ds, orient="h", palette="Set1")
Python3
# import libraries
import seaborn as sns
import matplotlib.pyplot as plt
# load dataset
ds = sns.load_dataset("tips")
plt.figure(figsize=(8, 6))
# use palette method
ax = sns.boxplot(data=ds, orient="h", palette="Set1")
第 2 步:加载数据集以生成箱线图。
蟒蛇3
# loading dataset
ds = sns.load_dataset('iris')
第 3 步:使用boxplot()方法生成箱线图。
蟒蛇3
# create boxplot object
ax = sns.boxplot(data=tips, orient="h")
第 4 步: Seaborn boxplot()函数有调色板参数,在这个例子中我们设置了Palette=”Set1″,它使用定性调色板 Set3 为 boxplot中的框着色。所以在boxplot方法中添加调色板参数。
蟒蛇3
# use palette method
ax = sns.boxplot(data=ds, orient="h", palette="Set1")
以下是基于上述方法的完整程序:
蟒蛇3
# import libraries
import seaborn as sns
import matplotlib.pyplot as plt
# load dataset
ds = sns.load_dataset("tips")
plt.figure(figsize=(8, 6))
# use palette method
ax = sns.boxplot(data=ds, orient="h", palette="Set1")