plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.08k stars 2.54k forks source link

Annotation text not sticking to hline object on secondary_y axis #2960

Closed Maarten-vr closed 2 months ago

Maarten-vr commented 3 years ago

I was working with the recently introduced horizontal lines (hline) objects and believe I encountered a small bug. When adding a horizontal line to the secondary axis, the position of the annotated text seems to stick to the first y axis still.

Screenshot 2020-12-08 at 10 52 00

Working Example:

#Create Figure From Scratch
import numpy as np
from plotly.subplots import make_subplots
fig = make_subplots(specs=[[{"secondary_y": True}]], shared_xaxes=True)

# Add traces
serie_1 = [3000,3500,4000,3500,3000,3500]
serie_2 = [20,25,30,25,20,25]
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=serie_1, name='First Axis'), secondary_y=False)
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=serie_2, name='Second Axis'), secondary_y=True)

# Update X-Axis
fig.update_xaxes(nticks=5)

# Update First Y-Axis
fig.update_yaxes(title="First Axis",
                 nticks=7,
                 range=[0,5000],
                 secondary_y=False)

fig.add_hline(y=np.mean(serie_1),
              line_dash='dash', 
              line_color='blue', 
              annotation_text= 'average blue',
              annotation_position='top right', 
              annotation_font_color='blue')

# Update Secondary Y-Axis
fig.update_yaxes(title="Second Axis",
                 nticks=7,
                 range=[0,50],
                 secondary_y=True)

fig.add_hline(y=np.mean(serie_2),
              line_dash='dash', 
              line_color='red',
              annotation_text= 'average red',
              annotation_position='top right', 
              annotation_font_color='red',
              annotation_yref="y2", #not working?
              secondary_y=True)

Am I overlooking something or is this a small bug?

PS: Great addition by the way!

fwhigh commented 3 years ago

Seeing the same issue, thanks for reporting

Jamesong7822 commented 3 years ago

I have the same issue as well!

mariostrd commented 2 years ago

Same here!

Lundez commented 2 years ago

+1, I've got the same issue

MoritzBlumer commented 1 year ago

Same issue here

SorosBot commented 1 year ago

It worked this way:

fig.add_hline(name='any name', y=50, yref='y2', line=dict(color='darkred', width=1, dash='dash'), annotation=dict( text="any text", yanchor="bottom", xanchor="right", font_color="darkred")

gvwilson commented 2 months ago

Hi - we are trying to tidy up the 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. Alternatively, if it's a request for tech support, please post in our community forum. Thank you - @gvwilson