在下面的例子中,通过'subject'和'group'制作并索引了许多观察结果X和Y.
我想在FacetGrid
中绘制所有X和Y观测值的排序值,其中FacetGrid的列是组。
现在,FacetGrid为所有子图分配xtick标签,如FacetGrid
中的最后一个子图。这是错误的,因为每个主题只在一个组中。
如何确保它是单个x轴上的“正确”主题?
%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Custom plot to plot sorted barplot
def sortedplot(x,y, **kwargs):
ax = plt.gca()
data = kwargs.pop("data")
data.sort_values('value').plot(kind='bar',x=x, y=y, ax=ax,**kwargs)
# testdata of multi indexed random data
df = pd.DataFrame(np.random.randint(0,200,size=(100, 2)),
columns=list(('X','Y')))
index = np.array(zip(np.arange(0,201),np.tile(np.array(['A','B','C','D']),25)))
df['subject'] = index.flatten()[0::2]
df['group'] = index.flatten()[1::2]
df = df.set_index(['subject','group'])
# melt the test dataframe
meltdf = pd.melt(df.reset_index(),id_vars=['subject','group'], var_name='PEAK')
sns.set(font_scale=2)
g = sns.FacetGrid(meltdf,col='group',row='PEAK',sharey=False, size=6, aspect=1.3)
g.map_dataframe(sortedplot, 'subject', 'value')