has2k1 / plotnine

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

Is there a way to add images onto the x axis? #817

Open zarthisius opened 1 week ago

zarthisius commented 1 week ago

Basically title. Is there a way within plot nine to add images in place of labels to the x axis tick positions?

I know you can get the matplotlib figure and axis using .draw() and matplotlib has this functionality using OffsetImage() but I am having a hard time actually implementing this.

Any help would be greatly appreciated

has2k1 commented 1 week ago

There isn't an easy way to do it. If/when we make it possible, we would want it to be much simpler than OffsetImage is for matplotlib.

Please post some examples (examples, mockups, etc) of what you want to do, they will help when working on the feature.

zarthisius commented 1 week ago

Thanks for the quick reply!

Sure, following is a minimum working example and what I want to be able to do

df = pd.DataFrame({'countries': ["Norway", "Spain", "Germany", "Canada", "China"], 'values': [10, 20, 30, 22, 19]})
p = (
    ggplot(data=df)
    +aes(x='countries', y='values')
    +geom_bar(stat='identity')
)

p.draw()

This gives the following graph image

But instead I would want to be able to generate a graph similar to T8CMy Where the images of the countries flags are provided separately

Just to make it easier to work on and consolidated here the links to the country flag images that work with my above example:

Canada China Germany Norway Spain

Using images directly from hyperlinks would be cool but ideally I want to use custom images stored locally.

I also believe ggplot2 has this functionality via the ggtext package as shown in the second example of this link: ggtext

On a side note: you mentioned there is not an easy way but is any way possible for the time being even if somewhat convoluted to achieve this? Would love for any guidance towards implementing something similar with the current combination of plotnine and matplotlib packages

Thanks again!