Skip to content

Textangle breaks when the figure size changes. #4858

Open
@CompRhys

Description

@CompRhys

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2considered for next cyclebugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions