0

I would like to have my image set as a background, that the picture is also covering title and legend and axis not only in image.

Is this possible in python?

I am using plotly to plot as for now, but am open to other tools if they work better. Can I change the settings in fig.add_layout_image to do so?

import plotly.express as px
color_dict={'Djurgården': 'dodgerblue', 
            'Malmö FF': 'lightcyan',
            'Hammarby': 'lawngreen',
            'Sirius': 'darkblue',
            'AIK': 'black',
            'Elfsborg': 'goldenrod',
            'Norrkoping': 'lightblue',
            'Mjällby': 'lightgoldenrodyellow',
            'Varbergs BoIS': 'darkgreen',
            'Halmstad': 'mediumblue',
            'Degerfors': 'darkred',
            'Kalmar': 'indianred',
            'BK Häcken': 'yellow',
            'Östersunds FK': 'red',
            'Örebro SK': 'darkgray'
           }
fig = px.scatter(df2, x=statx, y=staty, color="Team",
                 size="MP", color_discrete_map=color_dict)

fig.add_annotation(text="Magnus Eriksson",
                  xref="paper", yref="paper",
                  x=0.16, y=1, showarrow=False, bgcolor="black", font=dict(
            
            color="white"
            ))

fig.add_annotation(text="Mohanad Jeahze",
                  xref="paper", yref="paper",
                  x=0.225, y=0.9, showarrow=False, bgcolor="black", font=dict(
            color="white"
            ))

fig.add_annotation(text="Darijan Bojanic",
                  xref="paper", yref="paper",
                  x=0.48, y=0.8, showarrow=False, bgcolor="black", font=dict(
            color="white"
            ))

fig.add_annotation(text="Moustafa Zeidan",
                  xref="paper", yref="paper",
                  x=0.55, y=0.6, showarrow=False, bgcolor="black", font=dict(
            color="white"
            ))

fig.add_annotation(text="Akinkunmi Amoo",
                  xref="paper", yref="paper",
                  x=0.8, y=0.55, showarrow=False, bgcolor="black", font=dict(
            color="white"
            ))

fig.add_annotation(text="Tashreeq Matthews",
                  xref="paper", yref="paper",
                  x=0.8, y=0.475, showarrow=False, bgcolor="black", font=dict(
            color="white"
            ))

fig.add_annotation(text="Edward Chilufya",
                  xref="paper", yref="paper",
                  x=0.82, y=0.375, showarrow=False, bgcolor="black", font=dict(
            color="white"
            ))

fig.add_annotation(text="Patrick Wålemark",
                  xref="paper", yref="paper",
                  x=0.975, y=0.32, showarrow=False, bgcolor="black", font=dict(
            color="white"))

fig.update_layout(
    #title="Allsvenskan 2021: xT Passing vs xT Dribble",
    title={
        'text': "Allsvenskan 2021: xT Passing vs xT Dribble",
        'y':0.95,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'},
    xaxis_title="Normalized Value: xT Dribbles per 90",
    yaxis_title="Normalized Value: xT Passes per 90",
    legend_title="Teams Allsvenskan 2021",
)

img_width = 1
img_height = 1
scale_factor = 2.5

fig.add_layout_image(
    dict(
        sizex=img_width * scale_factor,
        y=img_height * scale_factor,
        sizey=img_height * scale_factor,
        xref="x",
        yref="y",
        opacity=1.0,
        layer="below",
        sizing="stretch",
        source="LJUS_BG_Playmaker.png")
)
fig.show()
´´´
Jesper
  • 1
  • I think it is possible because there are [official examples](https://plotly.com/python/images/#zoom-on-static-images). You can use an image library to load locale images, or you can [encode](https://stackoverflow.com/questions/53432151/plotly-layout-with-background-image-from-local-file) the image into a string and use it. – r-beginners Mar 28 '22 at 09:01

1 Answers1

0

try this.

backgroundImage = PhotoImage("D:\Documents\example.png")
Coozywana
  • 27
  • 10