调整散点图中的文本位置和旋转

问题描述 投票:0回答:1

我有下面的情节。我想将文本放在标记上方 20px 并将其旋转 25 度。

import plotly.graph_objects as go
import pandas as pd
    
fig = go.Figure()

data = [[1,2.3567],
        [2,5.45678],
        [3,1.45678],
        [4,7.45678],
        [5,4.2345]]

df = pd.DataFrame(data,columns=['x','y'])
fig.add_trace(go.Scatter(
    x=df['x'],
    y=df['y'],
    mode="lines+markers+text",
    name="Lines and Text",
    text=df['y'],
    textfont=dict(
        family="sans serif",
        size=28,
        color="LightSeaGreen"
    )
))

fig.update_layout(showlegend=False)

fig.show()

电流输出:

python plotly
1个回答
0
投票

一种方法是使用文本注释,例如。

fig.add_trace(go.Scatter(
    x=df['x'],
    y=df['y'],
    mode="lines+markers",
    name="Lines and Text"
))

for row in df.itertuples():
    fig.add_annotation(
        x=row.x,
        y=row.y,
        text=row.y,
        textangle=-25,
        font=dict(
            family="sans serif",
            size=28,
            color="LightSeaGreen"
        ),
        showarrow=False,
        yshift=20
    )

另请参阅 Python 图参考:layout.annotations

© www.soinside.com 2019 - 2024. All rights reserved.