0
votes

I am plotting a scatter plot demonstrating all NFL teams' defense. Instead of their names in plain text, I want to show their images on the bottom axis. Is it possible to do so? Here is the code snippet that I could come up with:

fig = px.scatter(df, x='defensiveTeam', y='passResult')
fig.show()

Note that my images are stored locally. enter image description here

1

1 Answers

1
votes

As far as I could find, I could not find a way to place the image on the x-axis. It is possible to place an image on the scatter points with the following code, which erases the ticks on the x-axis but should show them on your graph.

import plotly.express as px
import base64

x=[0, 1, 2, 3, 4]
y=[2, 1, 4, 9, 16]

fig = px.scatter(x=x, y=y)

team_logos = ['arizona_cardinals.png','atlanta_falcons.png','carolina_panthers.png','chicago_bears.png','dallas_cowboys.png']

fig.update_xaxes(tickvals=['','','','',''])
fig.update_layout(yaxis_range=[0,20])

# add images
for i,src,yy in zip(range(len(team_logos)),team_logos,y):
    logo = base64.b64encode(open('./data/'+src, 'rb').read())
    fig.add_layout_image(
        source='data:image/png;base64,{}'.format(logo.decode()),
        xref="x",
        yref="y",
        x=i,
        y=yy,
        xanchor="center",
        yanchor="bottom",
        sizex=3,
        sizey=3,
    )

fig.show() 

enter image description here