@plotmaster422
If you use fig.add_vrect for each subplot, then layer="below" is effective:
import numpy as np
import plotly.graph_objects as go
from scipy.stats import norm
from plotly.subplots import make_subplots
x = np.random.normal(1, 1.2, size=300)
X = np.linspace(1-3*1.2, 1+3*1.2, 100)
mypdf=norm.pdf(X, loc=1, scale=1.2)
import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="day", y="total_bill", color="smoker")
fig.update_traces(quartilemethod="exclusive");
figs=make_subplots(rows=1, cols=2)
figs.add_trace(fig.data[0], row=1, col=1)
figs.add_trace(fig.data[1], row=1, col=1)
figs.add_trace(go.Histogram(x=x, histfunc="count", histnorm= "probability density",
opacity= 0.7, marker_color= "rgba(4, 217, 255, 255)"), row=1, col=2)
figs.add_trace(go.Scatter(x=X, y=mypdf, mode="lines", line_color="rgba(4, 217, 255, 255)"), row=1, col=2)
figs.update_layout(template="plotly_white", #yaxis_showgrid=False, yaxis2_showgrid=False,
width=800, height=400, boxmode="group", showlegend=False, bargap=0.01)
figs.add_vrect(type="rect",
x0=-1, x1=4,
layer="below",
fillcolor="#faeee0",
xref='x1', yref='y1',
line_width=0,
row=1, col=1
)
figs.add_vrect(type="rect",
x0=-3, x1=5.5,
layer="below",
fillcolor="black",
xref='x2', yref='y2',
line_width=0,
row=1, col=2
)
The vrect width, i.e. x0 and x1, are set in boxplot by trial and error, while for histogram by observing the xaxis2 range.
This is the plot that displays yaxis, and yaxis2 grid lines :
and this one corresponding to yaxis_showgrid=False, yaxis2_showgrid=False (uncommmented in fig.update_layout):