Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
McDic opened this issue Nov 26, 2024 · 1 comment
Open
Labels
bug something broken P3 backlog

Comments

@McDic
Copy link

McDic commented Nov 26, 2024

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.

@McDic McDic changed the title fig.update_traces(xaxis=..) will destroy hline and vline objects on subplots fig.update_traces(xaxis=..) will destroy hline or vline objects on subplots Nov 26, 2024
@McDic
Copy link
Author

McDic commented Nov 28, 2024

I've found current bandaid patch.

  1. Put figure.update_traces(xaxis=..) before creating hlines.
  2. Put figure.update_shapes(xref="x domain", xsizemode="scaled", x0=0, x1=1) after you created hlines.

Then you are good to go.

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

    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

    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",
        },
    )

    # and more hlines...

    figure.update_shapes(xref="x3 domain", xsizemode="scaled", x0=0, x1=1)  # <----- UPDATED SHAPES XREF TO XAXIS DOMAIN
    return figure

Image

@gvwilson gvwilson added P3 backlog bug something broken labels Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P3 backlog
Projects
None yet
Development

No branches or pull requests

2 participants