情节散点图,避免重叠文本

时间:2021-04-19 20:21:38

标签: python plotly scatter-plot

我正在寻找一种解决方案来避免文本标签中的文本重叠。我创建了带有情节分散的图像。也许这里有一个自动化。

from pandas import util
import plotly.express as px
import plotly.graph_objects as go

df= util.testing.makeDataFrame()
df_keyfigures_all = df[['A','B']]



fig = px.scatter(df_keyfigures_all, x="A", y="B",size_max=60,
                     text=df_keyfigures_all.index)

fig.update_traces(textposition='top center')
fig.layout = go.Layout(yaxis=dict(tickformat=".0%"), xaxis=dict(tickformat=".0%"),
                       yaxis_title="A", xaxis_title="B")


fig.update_layout(showlegend=False)
plotly.io.write_image(fig, file='keyfigures.png', format='png')

enter image description here

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

不幸的是,似乎没有直接的方法可以做到这一点。在 plotly 社区论坛上稍微挖掘一下就会发现它有 already been requested 和问题的 developers are aware

答案 1 :(得分:0)

这不是完美的解决方案,但一种方法是隐藏悬停文本中的文本。

fig = px.scatter(df_keyfigures_all, x="A", y="B",size_max=60,
                 hover_name = df_keyfigures_all.index)