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

如何移除Plotly图表中color属性生成的标签

解决Plotly图例占用空间问题

你可以通过在update_layout()中设置showlegend=False来完全禁用由color属性生成的图例,具体修改如下:

修改后的完整代码:

fig = px.line(
    df_complete,
    x="Month",
    y="Count",
    color="Code Description",
    text="Count",  
    hover_data=[
        "Month",
        "Code Description",
        "Count",
    ], 
    line_shape="linear",  
)

fig.update_layout(
    title="Count of activities per month",
    xaxis=dict(title="Month"),
    yaxis=dict(title="Count"),
    showlegend=False  # 新增该行以禁用图例
)

fig.show()

如果只是不想让图例占用过多空间但不完全隐藏,也可以调整图例的位置、布局等属性优化显示,示例如下:

fig.update_layout(
    # 保留原有布局设置
    title="Count of activities per month",
    xaxis=dict(title="Month"),
    yaxis=dict(title="Count"),
    # 调整图例为横向并移至图表下方
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=-0.3,
        xanchor="center",
        x=0.5
    )
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:05:56