The parameter "layout='above'" in add_layout_image does not place the image above the plot

Hi everyone,

Thank a lot in advance to read my question.

In the following code, I would like the python icon to be above the red square, which is not the case as shown in the screenshot bellow. I am using the 5.19.0 plotly version on WSL.

import plotly.graph_objects as go

fig = go.Figure()

fig.add_shape(
    type="rect",
    x0=0, y0=0, x1=1, y1=1,
    fillcolor='red')
fig.add_layout_image(    
    source="https://images.plot.ly/language-icons/api-home/python-logo.png", 
    xref="x",                      
    yref="y",                      
    x=0.5,                           
    y=0.5,     
    layer="above",
    sizex=1,
    sizey=1
    )

fig.show()

Hey @AkinaSorrow welcome to the forums.

I guess the below and above refers to the plot itself and not necessarily to the β€œlast” layer you added.

In your case, setting layer="below" to the shape should show the image on top.

fig.add_shape(
    type="rect",
    x0=0, y0=0, x1=1, y1=1,
    fillcolor='red',layer="below")

Thank you !

That solve my problem.

Here is the corrected code:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_shape(
    type="rect",
    x0=0, y0=0, x1=1, y1=1,
    fillcolor='red',
    layer="below",)
fig.add_layout_image(    
    source="https://images.plot.ly/language-icons/api-home/python-logo.png", 
    xref="x",                      
    yref="y",                      
    x=0.5,                           
    y=0.5,     
    layer="above",
    sizex=1,
    sizey=1
    )

fig.show()

1 Like