seaborn画分类散点图,hue自动配合颜色一直在变

如何固定颜色设置
如用seaborn画分类散点图,用hue分类
第一张红色的点代表事业部
第二张红色的点又代表机关
第三张红色的点又代表保障部门
如何固定下来
红色代表 事业部
蓝色代表 机关
灰色代表保障部门

使用scatterplot中的参数palette进行设置,类似如下示例:

import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")

# Load the example diamonds dataset
diamonds = sns.load_dataset("diamonds")

# Draw a scatter plot while assigning point colors and sizes to different
# variables in the dataset
f, ax = plt.subplots(figsize=(6.5, 6.5))
sns.despine(f, left=True, bottom=True)
clarity_ranking = ["I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"]
sns.scatterplot(x="carat", y="price",
                hue="clarity", size="depth",
                #palette="ch:r=-.2,d=.3_r",
                palette=['red','green','blue','orange','yellow','cyan','pink','purple'],
                hue_order=clarity_ranking,
                sizes=(1, 8), linewidth=0,
                data=diamonds, ax=ax)
plt.show()


如有帮助,请采纳。点击我回答右上角【采纳】按钮。