python 无法格式化或删除px中的图例,

n3h0vuf2  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(143)

我试图移动散点图的图例,但似乎没有什么工作。
这是简单的散点图。

  1. fig_party_body = px.scatter(body_data, x= 'mean_stance', y = 'interest', color = 'mean_stance' )
  2. fig_party_body

字符串
这是散射结果:x1c 0d1x
现在我想移动图例,但是我不能用更新布局来做到这一点-例如,无论我做什么,这段代码都不会移动它(切换xanchor,摆弄x和y值):

  1. fig_party_body.update_layout(
  2. legend=dict(
  3. x=1.02, # Adjust the x position of the legend
  4. y=1.0, # Adjust the y position of the legend
  5. borderwidth=2, # Set legend border width
  6. xanchor='right', # Set x anchor to the right side of legend
  7. yanchor='top' # Set y anchor to the top of legend
  8. ))


甚至试图完全删除它,update_layout(showlegend = False)不起作用!它仍然存在。我做错了什么?这对我正在构建的其他更复杂的图形很重要。谢谢!

rfbsl7qr

rfbsl7qr1#

你想在问题上移动的是没有图例的颜色条吗?那么设置是有色标的颜色条。

  1. import plotly.express as px
  2. df = px.data.iris()
  3. fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')
  4. fig.update_layout(
  5. coloraxis=dict(colorbar=dict(
  6. x=1.02, # Adjust the x position of the legend
  7. y=1.0, # Adjust the y position of the legend
  8. borderwidth=2, # Set legend border width
  9. xanchor='right', # Set x anchor to the right side of legend
  10. yanchor='top' # Set y anchor to the top of legend
  11. ))
  12. )
  13. fig.show()

字符串


的数据

展开查看全部

相关问题