You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly图表外部注释不显示,求助添加侧边底部文本

解决方案

你的问题出在注释的定位参数和图表边距设置上,当前的y=-0.5超出了画布显示范围,且没有预留足够的底部空间。按以下步骤修改即可实现左侧底部的文本标注:

修改要点

  • 使用paper坐标系定位,避免受坐标轴范围影响
  • 调整位置到左侧底部,设置合适的偏移量
  • 增加图表底部边距,防止文本被截断
  • 调整字体大小到可读范围

修改后的完整代码

data1= final_api.query("info_title=='JupyterHub'").sort_values(by=['commitDate'])
data1['Year-Month'] = pd.to_datetime(data1['Year-Month']) 
data1['Commit-growth'] = data1['commits'].cumsum()
import plotly.graph_objects as go
import plotly.express as px

fig = px.scatter(data1, x='Year-Month', y='Commit-growth', color='major_version', text='Commit-growth')

fig.add_trace(go.Scatter(mode='lines',
                         x=data1["Year-Month"],
                         y=data1["Commit-growth"],
                         line_color='black',
                         line_width=1,
                         line_shape='hvh',
                         showlegend=False
                       )
             )

for _,row in data1.iterrows():
    fig.add_annotation(
        go.layout.Annotation(
            x=row["Year-Month"],
            y=row["Commit-growth"],
            text=row['info_version'],
            showarrow=False,
            align='center',
            yanchor='bottom',
            yshift=5,
            textangle=-10)
    )

# 修改后的注释设置
note = 'NYSE Trading Days After Announcement<br>Source: The NY TIMES Data: Yahoo! Finance'
fig.add_annotation(
    showarrow=False,
    text=note,
    font=dict(size=8),
    xref='paper',
    x=0,
    yref='paper',
    y=-0.15,
    align='left'
)

fig.update_layout(
    template='plotly_white',
    title_text='Version Change in Jupyter Hub API by commits',
    title_x=0.5,
    xaxis_title='Year-Month', 
    yaxis_title='Number of Commits', 
    yaxis_range=[0, 400],
    height=760, 
    width=1600, 
    xaxis_range=['2016-06-01', '2021-04-01'],
    margin=dict(b=80)
)

fig.update_traces(textposition="bottom right", showlegend=False,marker_size=10,marker_line_width=2, marker_line_color='black')

fig.show()

关键修改说明

  1. 坐标系切换:将xref和yref从x domain/y domain改为paper,定位基于整个画布的百分比,不会因坐标轴范围变化而偏移。
  2. 位置调整:x=0让文本左对齐画布左侧,y=-0.15将文本放在图表主体下方的空白区域,避免遮挡数据。
  3. 边距设置:margin=dict(b=80)增加底部边距,确保文本不会被画布边缘截断。
  4. 字体与对齐:调大字体并设置align='left',让文本更清晰且贴合左侧布局。

内容的提问来源于stack exchange,提问作者Brie MerryWeather

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 03:25:17