1

使用 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 进行说明):

在此处输入图像描述 谢谢!

4

1 回答 1

1

多亏了这个提示,我能够通过操纵 matplotlib 文本和背景对象来“分组”标签:

# 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")

fig = my_plot.draw()
fig._themeable['strip_text_x'][0].set_text('\n 0.0')
fig._themeable['strip_text_x'][1].set_text('model 1 \n 0.10')
fig._themeable['strip_text_x'][2].set_text('\n 0.20')
fig._themeable['strip_background_x'][0].set_width(3.0)

fig._themeable['strip_text_x'][3].set_text('\n 0.00')
fig._themeable['strip_text_x'][4].set_text('model 2 \n 0.10')
fig._themeable['strip_text_x'][5].set_text('\n 0.20')
fig._themeable['strip_background_x'][3].set_width(3.0)

fig._themeable['strip_text_x'][6].set_text('\n 0.00')
fig._themeable['strip_text_x'][7].set_text('model 3 \n 0.10')
fig._themeable['strip_text_x'][8].set_text('\n 0.20')
fig._themeable['strip_background_x'][6].set_width(3.0)

fig._themeable['strip_text_x'][9].set_text('\n 0.00')
fig._themeable['strip_text_x'][10].set_text('model 4 \n 0.10')
fig._themeable['strip_text_x'][11].set_text('\n 0.20')
fig._themeable['strip_background_x'][9].set_width(3.0)

plt.show()

结果如下所示:

Bildschirmfoto 2020-08-21 um 07 50 11

于 2020-08-21T05:56:00.587 回答