plotly / plotly/plotly.py

legend.maxheight not honored in Jupyter / notebook renderer though works in browser

Open
#5,378 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P2
Dominant language
Python
Stars
18.8k
Forks
2.8k
Avg merge
16h 26m
Merged PRs (30d)
21

Description

Description

I found that when using legend.maxheight in Plotly (>= 6.3), it works as expected when rendering via browser (e.g. fig.show(renderer="browser") or write_html). However, when using Jupyter Notebook (or similar notebook-based renderers), the maxheight setting is ignored / not reflected — the legend expands or overflows as if maxheight were not applied.

This inconsistent behavior suggests that the notebook renderer is not honoring legend.maxheight, which hampers consistent formatting of legends in notebook environments.


Code Example to Reproduce
import plotly.express as px
from plotly import data

df = data.gapminder().query("year==2007 and continent == 'Europe'")

fig = px.scatter(
    df,
    x="gdpPercap",
    y="lifeExp",
    color="country",
    size="pop",
    size_max=45,
    title="Life Expectancy vs. GDP per Capita in 2007 (by Country)",
    labels={"gdpPercap": "GDP per Capita"},
)

fig.update_layout(
    xaxis=dict(side="top"),
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=-0.35,
        xanchor="center",
        x=0.5,
        maxheight=0.005,  # comment out this line to see legend take up ~0.5 of plotting area
        title_text="Country"
    ),
)
# Display inline in notebook
fig.show()

# Display via browser
fig.show(renderer="browser")

When executed:

  • In browser renderer, the legend is constrained in height per the maxheight=0.005 setting (i.e. 0.5% of plotting height).
  • In notebook renderer, the legend is not constrained and expands beyond that, ignoring the maxheight.

Expected Behavior

The legend.maxheight setting should be honored consistently across all renderers, including the notebook-based inline renderers inside Jupyter or related environments. That is, legends should be constrained to the height specified by maxheight, even when showing inline.


Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Plotly Express example with fig.show() in a Jupyter notebook and with fig.show(renderer="browser"), comparing the legend behavior with write_html. Trace the notebook-based renderer entry point and determine why legend.maxheight is not applied; done means the notebook renderers constrain the legend consistently with the browser renderer.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.