plotly / plotly/plotly.py

Title moves off-page when adding new line with `<br>`

オープン
#4,309 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug P3
主要言語
Python
スター
18.8k
フォーク
2.8k
平均マージ
16時間 26分
マージ済み PR(30日)
21

説明

Description

When I am plotting normally, sometimes I want to add a new line to the title, and use that as a sub-title. When I do this, it will move the first line of my title up and off the page. This occurs even when I set title_y=1 and title_yref="container" and title_yanchor="top".

Reproducible Example

First, let's define the default plot.

from plotly import express as px
data = px.data.stocks(indexed=True, datetimes=True)
fig = (
    px.line()
    .add_scatter(
        name="Google",
        x=data.index,
        y=data["GOOG"],
        showlegend=True,
    )
    .add_scatter(
        name="Amazon",
        x=data.index,
        y=data["AMZN"],
        showlegend=True,
    )
    .add_scatter(
        name="Netflix",
        x=data.index,
        y=data["NFLX"],
        showlegend=True,
    )
)

When I run this with a title with only 1 line, it looks good:

fig1 = fig.update_layout(
    margin={"t": 40, "b": 10, "l": 10, "r": 10},
    title=dict(
        text="<b>Stock Prices</b>",
        x=0.5,
        xref="container",
        xanchor="center",
        y=0.99,
        yref="container",
        yanchor="top",
    ),
    xaxis_title="Date",
    yaxis_title="Values",
    legend_title="Stock",
)
fig1.show()

plot1

However, when I change only the title to add a second line, it will push the top of the title off the page.

fig2 = fig1.update_layout(title_text="<b>Stock Prices</b><br>For years 2018-2019")
fig2.show()

plot2

It will even have the same problem when I leave the margins as their default values.

fig3 = fig2.update_layout(margin=None)
fig3.show()

plot3

If I manually move the title downwards, it will resolve the issue. However, that is not the point here. Because the title should already be anchored to the top of the container at y level 0.99. I shouldn't need to manually move it downwards even more when I add a new line to the title.

fig4 = fig2.update_layout(
    margin={"t": 40, "b": 10, "l": 10, "r": 10},
    title_y=0.9,
)
fig4.show()

plot4

Conclusion

Something whacky is happening to the title whenever we set it to run with two lines. Even if we lock it to the top and with the reference to the container, it will still somehow be overwritten.

Please help to fix it.

Additional information

I include the schema files for each plot:

plot1_schema.json.txt
plot2_schema.json.txt
plot3_schema.json.txt
plot4_schema.json.txt

And the full script for entirely reproducible example:

Expand for full script
# Import
from plotly import express as px
import json

# Data
data = px.data.stocks(indexed=True, datetimes=True)

# Default figure
fig = (
    px.line()
    .add_scatter(
        name="Google",
        x=data.index,
        y=data["GOOG"],
        showlegend=True,
    )
    .add_scatter(
        name="Amazon",
        x=data.index,
        y=data["AMZN"],
        showlegend=True,
    )
    .add_scatter(
        name="Netflix",
        x=data.index,
        y=data["NFLX"],
        showlegend=True,
    )
)

# Figure 1: Works well
fig1 = fig.update_layout(
    margin={"t": 40, "b": 10, "l": 10, "r": 10},
    title=dict(
        text="<b>Stock Prices</b>",
        x=0.5,
        xref="container",
        xanchor="center",
        y=0.99,
        yref="container",
        yanchor="top",
    ),
    xaxis_title="Date",
    yaxis_title="Values",
    legend_title="Stock",
)
fig1.show()
fig1.write_image("plot1.png", engine="kaleido")
with open("plot1_schema.json.txt", "wt") as f:
    f.write(json.dumps(fig1.layout.to_plotly_json(), indent=2))

# Figure 2: Title off page
fig2 = fig1.update_layout(title_text="<b>Stock Prices</b><br>For years 2018-2019")
fig2.show()
fig2.write_image("plot2.png", engine="kaleido")
with open("plot2_schema.json.txt", "wt") as f:
    f.write(json.dumps(fig2.layout.to_plotly_json(), indent=2))

# Figure 3: Even works with margins are default
fig3 = fig2.update_layout(margin=None)
fig3.show()
fig3.write_image("plot3.png", engine="kaleido")
with open("plot3_schema.json.txt", "wt") as f:
    f.write(json.dumps(fig3.layout.to_plotly_json(), indent=2))

# Figure 4: Works if you manually move the title down; but that's not the point. It should be anchored to the top of the `container` already.
fig4 = fig2.update_layout(
    margin={"t": 40, "b": 10, "l": 10, "r": 10},
    title_y=0.9,
)
fig4.show()
fig4.write_image("plot4.png", engine="kaleido")
with open("plot4_schema.json.txt", "wt") as f:
    f.write(json.dumps(fig4.layout.to_plotly_json(), indent=2))

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue にある完全な再現可能な Python スクリプトから始め、特に fig1–fig4 の update_layout 呼び出しと、添付された plot1_schema.json.txt から plot4_schema.json.txt までのファイルを確認してください。1 行タイトルと複数行タイトルの挙動およびスキーマを比較してください。コンテナの上部に固定したときに、2 行のタイトルが完全に表示されたままになることをもって完了とします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
data-visualization
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。