【问题标题】:How can I change textposition on plotly based on the information?如何根据信息更改 plotly 上的文本位置?
【发布时间】:2021-07-16 02:47:05
【问题描述】:

我有这个代码:

data = go.Scatter(
          x=positionsX,
          y=positionsY,
          textposition='middle center',
          mode='markers+text',
          marker=dict(
            color=color,
            opacity=[1, 1, 1, 1, 1],
            size=[100, 70, 60, 30, 25]),
          text=list(sumByRegion.keys()),
        )

我想根据 sumByRegion.keys() 值将文本位置更改为“左下角”。

sumByRegion.keys() 是dict_values([55, 24, 16, 3, 2])

我现在在图像上:

image

编辑:

实际上,我正在寻找为每个单独项目设置文本位置。因此,我使用了一个 textposition 数组来解决这个问题。

data = go.Scatter(
          x=positionsX,
          y=positionsY,
          textposition=["middle center", "middle center", "middle center", "middle right", "middle left"],
          mode='markers+text',
          marker=dict(
            color=color,
            opacity=[1, 1, 1, 1, 1],
            size=[100, 70, 60, 30, 25]),
          text=list(sumByRegion.keys()),
        )

【问题讨论】:

  • 实际上,我正在寻找为每个单独的项目设置文本位置。因此,我使用了一个 textposition 数组来解决这个问题。 ``` data = go.Scatter( x=positionsX, y=positionsY, textposition=["中间中间", "中间中间", "中间中间", "中间右侧", "中间左侧"], mode='markers +text', marker=dict( color=color, opacity=[1, 1, 1, 1, 1], size=[100, 70, 60, 30, 25]), text=list(sumByRegion.keys() ), ) ```

标签: pandas numpy plotly scatter plotly-python


【解决方案1】:

如果您要显示的文本是字典格式,那么您可以将其转换为您要显示的格式。我不确定所需的格式是什么,但我添加了一个换行符并添加了一个百分号。

import plotly.graph_objects as go

sumByRegion = {'USA':55,'EU':24,'Asia':16,'Africa':2,'South America':3}
text = {'{}<br>{}%'.format(k,v) for k,v in zip(sumByRegion.keys(), sumByRegion.values())}

data = go.Scatter(
          x=positionsX,
          y=positionsY,
          textposition='bottom left',
          mode='markers+text',
          marker=dict(
            color=color,
            opacity=[1, 1, 1, 1, 1],
            size=[100, 70, 60, 30, 25]),
          text=text,
        )

【讨论】:

  • 如果我的回答对你有帮助,请考虑采纳为正确答案
  • 我已经编辑了这个问题。感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
相关资源
最近更新 更多