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

n3h0vuf2  于 5个月前  发布在  Python
关注(0)|答案(1)|浏览(43)

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

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

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

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


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

rfbsl7qr

rfbsl7qr1#

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

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')

fig.update_layout(
    coloraxis=dict(colorbar=dict(
        x=1.02,  # Adjust the x position of the legend
        y=1.0,  # Adjust the y position of the legend
        borderwidth=2,  # Set legend border width
        xanchor='right',  # Set x anchor to the right side of legend
        yanchor='top'  # Set y anchor to the top of legend
    ))
)

fig.show()

字符串


的数据

相关问题