Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zarthisius opened this issue Jul 1, 2024 · 6 comments
Open

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

zarthisius opened this issue Jul 1, 2024 · 6 comments
Labels

Comments

@zarthisius
Copy link

zarthisius commented Jul 1, 2024

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 has2k1 added the Feature label Jul 2, 2024
@has2k1
Copy link
Owner

has2k1 commented Jul 2, 2024

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
Copy link
Author

zarthisius commented Jul 3, 2024

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!

@TerryGamon
Copy link

Workaround, you can do it like this:

I my example you need country flag image files with .jpg in the same directory.
Please tell me if you need a github example.

(Sorry, I didn't care about optics)


import pandas as pd
import plotnine as p9
import matplotlib
import matplotlib.pyplot as plt


df = pd.DataFrame({'countries': ["Norway", "Spain", "Germany", "Canada", "China"], 
                   'values': [10, 20, 30, 22, 19]})
df['jpg'] = df['countries'] + '.jpg'
df['countries'] = pd.Categorical(values=df['countries'], categories=df['countries'], ordered=True)


p = (
    p9.ggplot(data=df)
    +p9.aes(x='countries', y='values')
    +p9.geom_bar(stat='identity')
    +p9.scale_y_continuous(limits=(-3,30))
    +p9.theme(figure_size=[16,9])
)

fig = p.draw()
ax = fig.axes[0]

for i, row in df.iterrows():
    img = matplotlib.offsetbox.OffsetImage(
        plt.imread(row['jpg']), 
        zoom = 0.2)
    ax.add_artist(matplotlib.offsetbox.AnnotationBbox(img,
        (i+1 ,-2)
        ,pad = 0.4
        ,frameon =False
        )
        )
fig

image

@TerryGamon
Copy link

See also my Issue: #869

@TerryGamon
Copy link

and of course you can use .png too

@zarthisius
Copy link
Author

I see thanks for the work around I managed to do what I wanted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants