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

add_hline() breaks when axis is shared #3209

Closed
ysmu opened this issue May 29, 2021 · 9 comments
Closed

add_hline() breaks when axis is shared #3209

ysmu opened this issue May 29, 2021 · 9 comments

Comments

@ysmu
Copy link

ysmu commented May 29, 2021

I'm currently using fig.update_xaxes(spikemode='across+marker') with fig.update_traces(xaxis='x2'), as suggested here, to draw spike a line across subplots. It was working great until I added some horizontal lines to my chart.

add_hline() and update_traces(axis='x2')

image

update_traces(axis='x2') without add_hline()

image

add_hline() without update_traces(axis='x2')

image

Repro:

import plotly.graph_objects as go
from plotly.subplots import make_subplots


fig = make_subplots(rows=2, cols=1)

fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name='(1,1)'), row=1, col=1)
fig.add_hline(1.5, row=1, col=1)

fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name='(1,2)'), row=2, col=1)
fig.add_hline(1.5, row=2, col=1)

fig.update_traces(xaxis='x2')
fig.update_xaxes(spikemode='across+marker')
fig.update_layout(height=600, width=600, title_text='specs examples')
fig.show()

Is this a bug or am I not supposed to share axis here?

@ysmu ysmu changed the title add_hline breaking shared axis add_hline() breaks when axis is shared May 29, 2021
@ivzap
Copy link

ivzap commented Aug 11, 2021

Having a similar issue with vrect shapes. I have a multi-row, 1 col subplot with synced xaxis using fig.update_traces(xaxis=x<last subplot row>). But, when I add vrect shapes, they only appear on one subplot despite specifying rows="all". I believe this is due to the synced x-axes.

Without fig.update_traces(xaxis=x<last subplot row>) (synced x-axes):
Screenshot (1)

With fig.update_traces(xaxis=x<last subplot row>) (synced x-axes):
Screenshot (2)

@nicolaskruchten
Copy link
Contributor

Right, so add_hline() across multiple subplots is designed to work only with subplots created by make_subplots without subsequent modifications to the assignment of traces to axes.

Do you get the behaviour you want if you add the rects/lines before updating the traces?

@ivzap
Copy link

ivzap commented Aug 13, 2021

No. Unfortunately the same behavior occurs regardless of when the shapes are drawn. If this is the way it is designed to behave, the row argument of add_hline() or add_vrect() is misleading. It seems to assign the shape to the axis of the row rather than the row itself.

Is there any other way you'd think I might be able to get away with having vrects and a spikeline that goes across all subplots?

Thanks for the suggestion though!

@nicolaskruchten
Copy link
Contributor

Try something like the following, to apply the same transformations to your lines and rects as you are applying to your traces:

fig.update_shapes(selector=dict(type="line"), xref="x2 domain")
fig.update_shapes(selector=dict(type="rect"), xref="x2")

@nicolaskruchten
Copy link
Contributor

(or whichever axis it is, perhaps x and x domain)... here's the output I get:

image

@ivzap
Copy link

ivzap commented Aug 16, 2021

Worked like a charm! Thank you so much; looks sick now

@AlanCPSC
Copy link

AlanCPSC commented Jan 24, 2024

@nicolaskruchten, Do you have any insights into why the x-axis is bugging out when I uncomment these lines? 🤔

Reproducible Code

import plotly.graph_objects as go
from plotly.subplots import make_subplots
from datetime import date

trace1 = go.Scatter(x=[date(2023, 1, 15), date(2023, 2, 15), date(2023, 3, 15), date(2023, 4, 15),
                       date(2023, 5, 15), date(2023, 6, 15), date(2023, 7, 15), date(2023, 8, 15),
                       date(2023, 9, 15), date(2023, 10, 15), date(2023, 11, 15), date(2023, 12, 15)],
                    y=[1000, 2000, 3000, 4000,
                       5000, 6000, 7000, 8000,
                       9000, 10000, 11000, 12000],
                    name='trace1')

trace2 = go.Scatter(x=[date(2023, 1, 15), date(2023, 2, 15), date(2023, 3, 15), date(2023, 4, 15),
                       date(2023, 5, 15), date(2023, 6, 15), date(2023, 7, 15), date(2023, 8, 15),
                       date(2023, 9, 15), date(2023, 10, 15), date(2023, 11, 15), date(2023, 12, 15)],
                    y=[10_000, 20_000, 30_000, 40_000,
                       50_000, 60_000, 70_000, 80_000,
                       90_000, 100_000, 110_000, 120_000],
                    name='trace2')

fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.02)

fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=2, col=1)

fig.add_annotation(
    xref='x domain',
    yref='y domain',
    x=0,
    y=1,
    text='FIRST ANNOTATION',
    showarrow=False,
    font=dict(size=14, color='blue'),
    row=1, col=1
)

fig.add_annotation(
    xref='x domain',
    yref='y domain',
    x=0,
    y=1,
    text='SECOND ANNOTATION',
    showarrow=False,
    font=dict(size=14, color='red'),
    row=2, col=1
)

# fig.update_traces(xaxis='x')
# fig.update_xaxes(spikemode='across+marker')
# fig.update_shapes(selector=dict(type="line"), xref="x domain")
# fig.update_shapes(selector=dict(type="rect"), xref="x")

fig.update_layout(height=600, width=600, title_text='Example Chart')
fig.show()

Chart Outputs

Before Uncommenting After Uncommenting
newplot_before newplot_after

@Coding-with-Adam
Copy link
Contributor

Hi @AlanCPSC
Can you please post this question on the community forum?

@gvwilson
Copy link
Contributor

Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants