我找到了每个子图带有图例的子图示例代码。我通过添加 row_heights 来更改它,现在图例不适合子图。
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, row_heights=[2,1,0.75])
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 = 100,
yaxis1_range=[50, 90],
yaxis2_range=[50, 90],
yaxis3_range=[50, 90]
)
fig.show()
现在我正在寻找一种解决方案来管理不同 row_heights 的 legend_tracegroupgap。我希望在子图旁边的顶部有图例。
从
Plotly v5.15
开始,您可以添加多个图例,并相对于绘图的高度定位它们。
在您的示例中,您可以将参数
legend='legend'
、legend='legend2'
或 legend='legend3'
添加到每个 go.Scatter
以将它们与图例组匹配,然后将参数 legend = {"y": 1.0}, legend2 = {"y": 0.42},legend3 = {"y": 0.08}
添加到 fig.update_layout
。
以下是完整代码和结果图:
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, row_heights=[2,1,0.75])
fig.append_trace(go.Scatter(
x=df.query("country == 'Canada'")['year'],
y=df.query("country == 'Canada'")['lifeExp'],
name = 'Canada',
legend='legend',
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',
legend='legend',
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',
legend='legend2',
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',
legend='legend2',
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',
legend='legend2',
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',
legend='legend3',
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',
legend='legend3',
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 = {"y": 1.0},
legend2 = {"y": 0.42},
legend3 = {"y": 0.08},
yaxis1_range=[50, 90],
yaxis2_range=[50, 90],
yaxis3_range=[50, 90]
)
fig.show()