简体   繁体   English

在不同的seaborn或matplotlib图上分配命名的Colormap /调色板

[英]Assigning a named Colormap / palette across different seaborn or matplotlib plots

I have the following dummy data. 我有以下虚拟数据。

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as pet

# Generate dummy data
a = np.random.random(70)
b = np.random.random(70) * 1.2
c = np.random.random(70) * 0.5 + 0.5
d = np.random.random(70) * 1.2 - 0.2

# Collate into a DataFrame
df = pd.DataFrame({'Control': a, 'Group1': b, 'Group2': c, 'Group3': d}) 
df = pd.melt(df) # Reshapes the data to allow for easy plotting with seaborn
df.columns = ['Group', 'value']

and I want to create 2 plots. 我想创建2个地块。

# Plot all data
sb.swarmplot(data = df, x = "Group", y = "value")

在此处输入图片说明

and

# Plot all data except `Group1`
sb.swarmplot(data = df[df["Group"] != "Group1"], x = "Group", y = "value")

在此处输入图片说明

As you can see, the color mapping between the 2 plots is inconsistent. 如您所见,这两个图之间的颜色映射不一致。 How do I create a named Colormap or palette that can be parsed to the seaborn commands so the category-color mapping can be preserved? 如何创建可以解析为seaborn命令的命名Colormap或调色板,以便可以保留类别-颜色映射?

Thank you in advance! 先感谢您!

You can pass a dictionary: 您可以通过字典:

pal = dict(Control="k", Group1="b", Group2="g", Group3="r")
sns.swarmplot(..., palette=pal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM