Mixed subplots with one subplot being a picture possible?

Hello,

I use the mixed subplots like this code. Is it possible to add a picture as a subplot?

@Gobrel
Yes, you can add an image to a subplot cell. Fisrt define an auxiliary figure, figm, via px.imshow(image), and add
figm.data[0] to the corresponding subplot:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
import skimage.io as sio
import numpy as np
from numpy import pi, sin, cos

img =sio.imread("sun-flower.jpg")
figm= px.imshow(img)

fig = make_subplots(
    rows=2, cols=2, horizontal_spacing=0.05, vertical_spacing=0.075,
    specs=[[{"type": "scatter"}, {"type": "bar"}],
           [{"type": "image"}, {"type": "surface"}]])


fig.add_trace(go.Scatter(x=[1,2,3], y=[0.5, 1,0.7]), 1,1)
fig.add_trace(go.Bar(x=["A", "B", "C", "D"], y=[11, 7, 4, 2]), 1,2)

fig.add_trace(figm.data[0], 2, 1)
x, y = np.meshgrid(np.linspace(-pi/2, pi/2, 100), np.linspace(-pi/2, pi/2, 100))
fig.add_trace(go.Surface(x=x[0, :], y=y[:, 0], z=cos(x+y)*sin(x*y), showscale=False), 2,2)
fig.update_layout(title_text="Mixed subplots with image", title_x=0.5,width=700, height=500, 
                  showlegend=False, font_size=11)
fig.update_scenes(camera_eye=dict(x=1.7, y=1.7, z=1))
fig.update_layout(xaxis3_visible=False, yaxis3_visible=False)

2 Likes