Hi all,
I do not have the perfect solution but if you are facing this issue in Python you can utilize the fields legendgroup
and legend_tracegroupgap
to fake having individual legends per subplot. See my explanation here: [Plotly] How to make individual legends in subplot | Kaggle
You can try the code below to create the figure above
import pandas as pd
import plotly.express as px
df = px.data.gapminder().query("continent=='Americas'")
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=3, cols=1)
fig.append_trace(go.Scatter(
x=df.query("country == 'Canada'")['year'],
y=df.query("country == 'Canada'")['lifeExp'],
name = 'Canada',
legendgroup = '1'
), row=1, col=1)
fig.append_trace(go.Scatter(
x=df.query("country == 'United States'")['year'],
y=df.query("country == 'United States'")['lifeExp'],
name = 'United States',
legendgroup = '1'
), row=1, col=1)
fig.append_trace(go.Scatter(
x=df.query("country == 'Mexico'")['year'],
y=df.query("country == 'Mexico'")['lifeExp'],
name = 'Mexico',
legendgroup = '2'
), row=2, col=1)
fig.append_trace(go.Scatter(
x=df.query("country == 'Colombia'")['year'],
y=df.query("country == 'Colombia'")['lifeExp'],
name = 'Colombia',
legendgroup = '2'
), row=2, col=1)
fig.append_trace(go.Scatter(
x=df.query("country == 'Brazil'")['year'],
y=df.query("country == 'Brazil'")['lifeExp'],
name = 'Brazil',
legendgroup = '2'
), row=2, col=1)
fig.append_trace(go.Scatter(
x=df.query("country == 'Argentina'")['year'],
y=df.query("country == 'Argentina'")['lifeExp'],
name = 'Argentina',
legendgroup = '3'
), row=3, col=1)
fig.append_trace(go.Scatter(
x=df.query("country == 'Chile'")['year'],
y=df.query("country == 'Chile'")['lifeExp'],
name = 'Chile',
legendgroup = '3'
), row=3, col=1)
fig.update_layout(
height=800,
width=800,
title_text="Life Expectancy in the Americas",
xaxis3_title = 'Year',
yaxis1_title = 'Age',
yaxis2_title = 'Age',
yaxis3_title = 'Age',
legend_tracegroupgap = 180,
yaxis1_range=[50, 90],
yaxis2_range=[50, 90],
yaxis3_range=[50, 90]
)
fig.show()