plotly / plotly/plotly.py

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

未关闭
#4,904 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug P3
主要语言
Python
星标
18.8k
派生
2.8k
平均合并
16 小时 26 分钟
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先使用 make_subplots、add_hline 和 figure.update_traces(xaxis="x3") 重现该问题;链接的 visualize.py 提供了完整示例。跟踪 update_traces 如何更改坐标轴分配,以及 subplot hline 对象如何表示。完成的标准是:更新 trace 坐标轴后,所有 subplot 中的每个 hline 和 vline 都得以保留,并且报告中的示例仍能显示它们。

由索引模型根据 Issue 内容生成。

评估

技术栈
plotly, python
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。