Black Lives Matter. Please consider donating to Black Girls Code today.

Background images in subplots

I’m trying to make some subplots of which one has a map as background so I have a nice overview of some coordinates. I managed to do it on a single chart but I can’t figure out how to do it on subplots. I tried something like this:

from plotly.subplots import make_subplots
import plotly.graph_objs as go
import plotly.offline as py

x = [1, 2, 3]
y = [4, 5, 6]

fig = make_subplots(rows=1, cols=2)

fig.add_trace(go.Scatter(x=x,y=y), row=1, col=1)
fig.add_trace(go.Scatter(x=y,y=x), row=1, col=2)

fig.add_layout_image(
source=“Orientation_map.png”,
xref=“x”,
yref=“y”,
x=0,
y=5,
sizex=5,
sizey=5,
sizing=“stretch”,
opacity=1,
layer=“below”,
row=1, col=1
)

py.plot(fig)

Which does not work unfortunately. When I made a single chart with a background I used go.Layout () (as in the given plotly examples) but I’m not sure how to get from there to backgrounds in a subplot. Anyone has some tips to make the magic happen?

-edit
The code actually seems to work, it just does not want to load a picture that is saved on my computer. When I replace the filename Orientation_map.png with an URL to the same image, everything works just fine. How can I get it to load a local image?

Hi @tabletop, welcome to the forum! You can also pass a PIL image as the source parameter, like

from PIL import Image
img = Image.open('Orientation_map.png')

and then pass img as the source parameter. And yes we should add this to https://plot.ly/python/images/ :-).

Thank you very much, it works like a charm!