plotly / plotly/plotly.py

`fig.update_traces(xaxis=..)` will destroy `hline` or `vline` objects on subplots

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

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

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

説明

Summary

When the figure is created by make_subplots, updating traces' xaxis or yaxis will destroy hline or vlines from user's view.

Details

The following picture represents my figure created with subplots of 3 rows and 1 column. It has horizontal line added by figure.add_hline(y=0.0, ...).

plot1_no_update_traces

def get_historical_charts(
    tournaments: list[TournamentSummary],
    max_data_points: int = 2000,
    window_sizes: tuple[int, ...] = DEFAULT_WINDOW_SIZES,
):
    # .... some dirty codes..

    figure.add_hline(    # <-------------------------------- ADDED hline
        y=0.0,
        line_color="red",
        line_dash="dash",
        row=1,
        col=1,
        label={
            "text": "Break-even",
            "textposition": "end",
            "font": {"color": "red"},
            "yanchor": "top",
        },
    )
    figure.update_layout(
        title="Historical Performance",
        hovermode="x unified",
        yaxis1={"tickformat": "$"},
        yaxis2={"tickformat": "%"},
        yaxis3={"tickformat": "$"},
    )
    figure.update_yaxes(row=3, col=1, patch={"type": "log"})
    # figure.update_traces(xaxis="x3")  # <------------------------------------- WILL CHANGE HERE
    return figure

However, I wanted to display vertical dash line on all subplots regardless of which graph you are focusing. I googled and found figure.update_traces(xaxis="x3") will make this work, but it destroyed hline. Notice that red dash line is disappeared on the second picture.

plot2_update_traces

def get_historical_charts(
    tournaments: list[TournamentSummary],
    max_data_points: int = 2000,
    window_sizes: tuple[int, ...] = DEFAULT_WINDOW_SIZES,
):
    # .... some dirty codes..

    figure.add_hline(    # <-------------------------------- ADDED hline
        y=0.0,
        line_color="red",
        line_dash="dash",
        row=1,
        col=1,
        label={
            "text": "Break-even",
            "textposition": "end",
            "font": {"color": "red"},
            "yanchor": "top",
        },
    )
    figure.update_layout(
        title="Historical Performance",
        hovermode="x unified",
        yaxis1={"tickformat": "$"},
        yaxis2={"tickformat": "%"},
        yaxis3={"tickformat": "$"},
    )
    figure.update_yaxes(row=3, col=1, patch={"type": "log"})
    figure.update_traces(xaxis="x3")  # <------------- UPDATED XAXIS AND hline IS DESTROYED
    return figure

Note that there is only 1 line of difference from above two codes.

For full codes, you can refer my repository's code.

For bandaid, I can do figure.update_traces(xaxis="x1") instead, but it will destroy custom objects on second/third subplot. So this is not a fundamental resolution.

Personally I think showing horizontal or vertical dash line to indicate which x or y are you hovering is important. However, with current status you can potentially destroy custom plot objects like hline if you do that.

Thanks for reading this.

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

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

はじめの一歩

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

調査の方向性

まず、make_subplots、add_hline、figure.update_traces(xaxis="x3") を使って問題を再現してください。リンクされている visualize.py に完全な例があります。update_traces が軸の割り当てをどのように変更するか、また subplot の hline オブジェクトがどのように表現されるかを追跡してください。トレースの軸を更新してもすべての subplot で hline と vline が保持され、報告された例で引き続き表示されれば完了です。

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

評価

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

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

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