has2k1 / plotnine

A Grammar of Graphics for Python

Home Page:https://plotnine.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

abnormal legend

wangjiawen2013 opened this issue · comments

Hi,
I made a figure using:

ggplot(obs,aes(x='library_id',y='percentage',fill='leiden')) + geom_bar(stat='identity') + scale_fill_manual(values=adata.uns['leiden_colors']) + theme(axis_text_x=element_text(angle=45))

The legend is weired, some items were lost (1, 19, 20, and so on):
image

When using plotnine_prism() with the same data, it looks well:
image

I don't want to use plotnine_prism() theme, so how to adjust the legend ?

You can set the number of columns that works for you

+ guides(fill=guide_legend(ncol=2))

and/or adjust the height of the keys

+ theme(legend_key_height=10)

Thanks, I'll try it.
And can plotnine adjust the columns automatically according to the number the keys ? When there are less keys, the legend is one column, while it's two columns when there are lots keys ?

Thanks, I'll try it. And can plotnine adjust the columns automatically according to the number the keys ? When there are less keys, the legend is one column, while it's two columns when there are lots keys ?

No it does not, but it is something we could look into. While I don't think the problem of the overflowing legend can go away entirely, it could become an even rarer edge-case.