2016-05-31 13 views
0

我有以下虚拟数据。在不同的seaborn或matplotlib地块上分配一个命名的Colormap /调色板

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'] 

我想创建2个图。

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

enter image description here

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

enter image description here

正如你可以看到,2个地块之间的颜色映射是不一致的。如何创建可以解析为seaborn命令的命名的Colormap或调色板,以便保留类别颜色映射?

预先感谢您!

回答

1

你可以通过字典:

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

我想通了几分钟,您发布之前!哈哈 –

相关问题