如何移除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
相关产品推荐
相关产品推荐

