我有一个三列的熊猫数据框:'Ethnicity', 'Locus', 'Count'
。
当我在jupyter笔记本中渲染图时,该图被合并。我想显示四个不同的情节。
也就是说,我想绘制每个“种族”的轨迹(x轴)与计数(y轴)。我想将其显示为构面,因此在同一视觉中分别显示为四个图。
示例代码
plt.bar(appended_data[appended_data['Ethnicity']==1]['Locus'].values,
appended_data[appended_data['Ethnicity']==1]['Count'].values)
而不是上面的代码中的“ 1”,我希望它是一个像“ i”之类的动态变量。
目前,通过粗略解决方案,是将上述代码行放入不同的“ jupyter”单元中,并手动更改[appended_data ['Ethnicity'] == 1]值。
有没有一种方法可以渲染四个图(最好在2 * 2网格中)?
也就是说,
plot1 for Ethnicity = 0, plot2 for Ethnicity = 1
plot1 for Ethnicity = 2, plot2 for Ethnicity = 3
来自以下示例数据:
appended_data= pd.DataFrame({'Ethnicity':[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
'Count': [1, 5, 8, 5, 4, 9, 4, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 5, 6, 9, 2, 5,
2, 1, 1, 3, 2, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 3, 2, 8, 1, 1, 2,
6, 1, 2, 7, 1, 3, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 8, 2, 8, 4, 5, 1,
3, 2, 3, 2, 1, 1, 2, 3, 1, 1, 2, 1],
'Locus':['13,12', '11,12', '10,10', '12,11', '12,12', '10,12', '10,11',
'12,10', '11,11', '11,7', '11,13', '11,10', '12,7', '12,13',
'13,10', '10,8', '9,10', '11,11', '12,11', '10,12', '13,13',
'12,12', '11,13', '10,13', '12,13', '11,10', '11,12', '12,10',
'9,12', '10,10', '9,11', '13,10', '7,12', '7,10', '9,10', '10,11',
'13,8', '11,7', '10,11', '7,12', '10,10', '13,10', '12,7', '11,12',
'11,11', '11,7', '11,10', '10,12', '9,12', '12,11', '12,8', '8,10',
'12,12', '12,9', '13,11', '14,10', '7,11', '8,12', '10,7', '10,8',
'12,12', '13,11', '10,11', '11,10', '11,12', '8,11', '12,10',
'13,10', '13,12', '12,11', '11,11', '12,7', '10,10', '10,13',
'11,14', '11,7', '10,12', '12,9']})
答案 0 :(得分:0)
import seaborn as sns
appended_data= pd.DataFrame({'Ethnicity':[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
'Count': [1, 5, 8, 5, 4, 9, 4, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 5, 6, 9, 2, 5,
2, 1, 1, 3, 2, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 3, 2, 8, 1, 1, 2,
6, 1, 2, 7, 1, 3, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 8, 2, 8, 4, 5, 1,
3, 2, 3, 2, 1, 1, 2, 3, 1, 1, 2, 1],
'Locus':['13,12', '11,12', '10,10', '12,11', '12,12', '10,12', '10,11',
'12,10', '11,11', '11,7', '11,13', '11,10', '12,7', '12,13',
'13,10', '10,8', '9,10', '11,11', '12,11', '10,12', '13,13',
'12,12', '11,13', '10,13', '12,13', '11,10', '11,12', '12,10',
'9,12', '10,10', '9,11', '13,10', '7,12', '7,10', '9,10', '10,11',
'13,8', '11,7', '10,11', '7,12', '10,10', '13,10', '12,7', '11,12',
'11,11', '11,7', '11,10', '10,12', '9,12', '12,11', '12,8', '8,10',
'12,12', '12,9', '13,11', '14,10', '7,11', '8,12', '10,7', '10,8',
'12,12', '13,11', '10,11', '11,10', '11,12', '8,11', '12,10',
'13,10', '13,12', '12,11', '11,11', '12,7', '10,10', '10,13',
'11,14', '11,7', '10,12', '12,9']})
g = sns.FacetGrid(appended_data, col="Ethnicity", col_wrap=2)
g.map(sns.scatterplot, "Locus", "Count")