如何将 adjust_text 与 plotnine 一起使用?

How do I use adjust_text with plotnine?

我正在尝试调整 geom_text 标签,以免它们重叠。使用 nudge_x、nudge_y 和大小调整标签并没有给我一个非常令人满意的结果。我遇到了 adjust_text 包,但我没能在我的代码中实现它。

现在我的图表看起来像这样:

Graph

我尝试添加 adjust_text =True(见下文),这给了我以下错误。 "Parameters {'adjust_text'}, are not understood by either the geom, stat or layer."

(ggplot(nutrient_rel_item, aes(y='Share_calories', x='Share_biomass', color='Type',show_legend=False))
+stat_smooth( aes(y='Share_calories', x='Share_biomass'),method='lm',inherit_aes=False) 
+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text =True)
+facet_wrap('~CCC1',nrow=2)
+ scale_x_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ scale_y_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ geom_point(size=0.1) 
+ geom_path(aes(group='Item'), arrow= arrow(angle = 15, length= 0.1, type = "closed"))
+ labs(x='x', y='y')
+ theme(legend_position='none'))

您需要传递带有选项的字典来调整文本,因此首先使用所需的选项定义它,例如:

adjust_text_dict = {
    'expand_points': (2, 2),
    'arrowprops': {
        'arrowstyle': '->',
        'color': 'red'
    }
}

然后使用上面的 ggplot 命令启用它,但更改行

+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text =True)

+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text=adjust_text_dict)

话虽如此,您收到的错误消息表明 plotnine 无法识别选项本身。请注意,您至少需要版本 0.6.0 才能达到 运行.