has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
3.92k stars 210 forks source link

abnormal legend #757

Open wangjiawen2013 opened 4 months ago

wangjiawen2013 commented 4 months ago

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 ?

has2k1 commented 4 months ago

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)
wangjiawen2013 commented 4 months ago

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 ?

has2k1 commented 4 months ago

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.