使用 plotnine 的facet_grid
功能时有没有办法对标签进行分组?
例如,采用以下 DataFrame(我制作了示例,以便您可以重现我的问题。实际上,我的 DataFrame 要大得多,但结构几乎相同):
df_test = pd.DataFrame(data= {'rate_1': [0.0,0.1,0.2,0.3,0.0,0.1,0.2,0.3,0.3,0.2,0.1,0.0],
'rate_2': [0.0,0.1,0.2,0.3,0.0,0.1,0.2,0.3,0.3,0.2,0.1,0.0],
'rate_3':[0.0,0.1,0.2,0.3,0.0,0.1,0.2,0.3,0.3,0.2,0.1,0.0],
'rate_4': [0.0,0.1,0.2,0.3,0.1,0.2,0.3,0.0,0.2,0.3,0.0,0.1],
'samples': [50000, 100000, 50000, 100000,50000, 100000, 50000, 100000,50000, 100000, 50000, 100000],
'model': ['model 1', 'model 2', 'model 3', 'model 4','model 1', 'model 2', 'model 3', 'model 4','model 1', 'model 2', 'model 3', 'model 4'],
'metric': [0.5,0.4,0.3,0.2,0.5,0.4,0.3,0.2,1,0.3,0.4,0.3]})
我创建了以下函数来根据速率、样本量和模型绘制绝对度量:
# Plotting
my_plot = ggplot(data=df_test, mapping=aes('rate_1', 'rate_2', fill='metric'))
my_plot += geom_tile()
my_plot += theme(axis_text_x=element_text(rotation=90), figure_size=(16, 8), strip_background_x=element_text(width=1.))
my_plot += scale_x_continuous(breaks=[.0, .1, .2, .3, .4, .5])
my_plot += scale_y_continuous(breaks=[.0, .1, .2, .3, .4, .5])
my_plot += facet_grid('rate_3 + samples ~ model + rate_4', labeller=label_value)
my_plot += scale_fill_gradient2(midpoint=0, low='blue', mid="white", high="red")
my_plot
结果图如下所示:
在标记 facet_grid 时,有没有办法对模型名称(模型 1、模型 2、模型 3、模型 4)和 rate_4 进行分组?我正在寻找一个结果,使得“列”的名称如下所示(我使用 Excel 进行说明):