plotly.py
plotly.py copied to clipboard
Position of multiple legends is not retained in Jupyter Lab 4.0.10
Issue identified with Plotly 5.18.0 and Jupyter Lab 4.0.10. Example based on the accepted answer to this post.
❌ Jupyter Lab 4.0.10, interactive Plotly figure: multiple legend positions are not retained
✔️ Jupyter Lab 4.0.10, non-interactive Plotly figure: multiple legend positions are retained
✔️ Jupyter Notebook 7.0.7, interactive Plotly figure: multiple legend positions are retained
✔️ Jupyter Notebook 7.0.7, non-interactive Plotly figure: multiple legend positions are retained
Example code
See https://stackoverflow.com/questions/76936532/how-to-manage-legend-tracegroupgap-for-different-row-heights-in-subplots-in-plot.
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],
).show() # .show("png") for non-interactive
Hi @pjireland Thanks for reporting this. We've been having issues with Jupyter Lab version 4.
For now, the solution would be to run this on Jupyter Lab v3 or a Python IDE like PyCharm, VS Code or similar ones.
Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson