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

Textangle breaks when the figure size changes. #4858

Open
CompRhys opened this issue Nov 10, 2024 · 2 comments
Open

Textangle breaks when the figure size changes. #4858

CompRhys opened this issue Nov 10, 2024 · 2 comments
Labels
bug something broken P2 considered for next cycle

Comments

@CompRhys
Copy link
Contributor

The following snippet should produce a figure where the annotation is parallel to the line, however if the figure size changes the rotation of the annotation doesn't change.

import plotly.graph_objects as go
import math

# Create a figure
fig = go.Figure()

# Add some sample data
fig.add_trace(go.Scatter(x=[1, 2, 3, 4, 5], y=[10, 12, 15, 17, 20], mode="lines", name="Data"))

# Define the start and end points of the diagonal line
x0, y0 = 1, 11  # Start point
x1, y1 = 5, 19  # End point

# Calculate the angle for the annotation text
angle = math.degrees(math.atan2((y1 - y0), (x1 - x0)))

# Add a diagonal line using add_shape
fig.add_shape(
    type="line",
    x0=x0,
    y0=y0,
    x1=x1,
    y1=y1,
    line=dict(color="Green", width=2, dash="dash"),
)

# Calculate the midpoint position
mid_x = (x0 + x1) / 2
mid_y = (y0 + y1) / 2

# Annotate the line with rotated text
fig.add_annotation(
    x=mid_x,  # Midpoint of the line for annotation position
    y=mid_y,
    text="Diagonal Line",
    showarrow=False,
    textangle=angle,  # Rotate text to match line angle
    font=dict(color="Green"),
    xshift=10,  # Optional: Adjust this to fine-tune horizontal alignment
    yshift=-10  # Optional: Adjust this to fine-tune vertical alignment
)

fig.update_layout(title="Diagonal Line with Parallel Annotation")
fig.show()

Image
Image

@gvwilson gvwilson added bug something broken P2 considered for next cycle labels Nov 11, 2024
@gvwilson
Copy link
Contributor

thanks for the bug report @CompRhys - I'll see if we can get it into an upcoming release.

@CompRhys
Copy link
Contributor Author

It's not particularly obvious that this is a bug because it may simply be too involves to have the text angles rotate adaptively as the figure size is changed but would be great to have!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

2 participants