plotly / plotly/plotly.py

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

未關閉
#5,378 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

bug P2
主要語言
Python
星號
18.8k
分支
2.8k
平均合併
16 小時 26 分鐘
30 天內合併 PR
21

描述

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.


貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先,在 Jupyter notebook 中使用 fig.show() 執行提供的 Plotly Express 範例,並使用 fig.show(renderer="browser") 執行,比較圖例與 write_html 的行為。追蹤以 notebook 為基礎的 renderer 進入點,並判斷為什麼沒有套用 legend.maxheight;當 notebook renderer 以與瀏覽器 renderer 一致的方式限制圖例時,即視為完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
jupyter-notebook, python
領域
data-visualization
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。