【问题标题】:How to avoid overlapping text in a plotly scatter plot?如何避免在散点图中重叠文本?
【发布时间】:2021-07-14 01:18:42
【问题描述】:

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

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')

感谢您的帮助!

【问题讨论】:

    标签: python plotly scatter-plot


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,但我必须使用 pdf 格式的图像
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 2023-03-21
      • 2021-08-10
      • 2014-03-02
      • 2019-12-21
      • 2011-05-22
      相关资源
      最近更新 更多