Python_Plotly lib中标签的计算坐标

Python_Plotly lib中标签的计算坐标,python,pandas,label,position,plotly,Python,Pandas,Label,Position,Plotly,在下面的代码中,我在地图上有标记的标签 import plotly.express as px import plotly.graph_objs as go import pandas as pd rows=[['501-600','15','122.58333','45.36667'], ['till 500','4','12.5','27.5'], ['more 1001','41','-115.53333','38.08'], ] colmns=['

在下面的代码中,我在地图上有标记的标签

import plotly.express as px
import plotly.graph_objs as go
import pandas as pd

rows=[['501-600','15','122.58333','45.36667'],
      ['till 500','4','12.5','27.5'],
      ['more 1001','41','-115.53333','38.08'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

fig=px.scatter_geo(df,lon='longitude', lat='latitude',
                      color='bins',
                      opacity=0.5,
                      size='data',
                      projection="natural earth")

fig.update_traces(hovertemplate ='bins')

fig.add_trace(go.Scattergeo(lon=[float(d) + 5 for d in df["longitude"]],
              lat=[float(d) + 0 for d in df["latitude"]],
              text=df["data"],
              textposition="middle right",
              mode='text',
              showlegend=False))
fig.show()
我把这段代码:

float(d) + 5 for d in df["longitude"]],
lat=[float(d) + 0 for d in df["latitude"]]
使这些标签靠近标记。 但在地图调整大小的情况下,这些标签看起来很奇怪。 我相信,有可能把标签位置的条件,而不仅仅是绝对值
现在是这样,但是标签坐标和数据中的值之间存在某种依赖关系。

一种可能适合您的方法是用空格填充
df.data
值。例如,这里的数据在新变量
tag
中用三个空格填充

#tag=['15','4','41']
tag=”“+df['data'].astype(str)
图添加轨迹(go.Scattergeo(lon=df[“经度”],
纬度=df[“纬度”],
text=标记,
textposition=“右中”,
mode='text',
showlegend=False)
或使用
文本模板

fig.add_trace(go.Scattergeo(lon=df["longitude"],
              lat=df["latitude"],
              text=df["data"],
              textposition="middle right",
              mode='text',
              showlegend=False,
              texttemplate="   %{text}"
                           ))
更新:通过基于
df.data
指定可变宽度并使用正确对正,可以使用
texttemplate
进行进一步格式化,有关更多选项,请参阅。比如:

#文本模板宽度基于公式宽度=x/12+3和'>'右对齐
tt=[“%{text:>”+str(x/12+3)+“}”表示df['data']]中的x
图添加轨迹(go.Scattergeo(lon=df[“经度”],
纬度=df[“纬度”],
text=df[“数据”],
textposition=“右中”,
mode='text',
showlegend=False,
texttemplate=tt
))
原件:

缩放:


这是一个有趣的选择,但我认为应该可以使用依赖关系,使cirlce的大小在%以内,因为所有大小都不同,而且当您缩放时,它会更加可见。图形与标记中心/标记边框的距离应相同。
textposition=“center”
是选项吗?使用
sizeref
,如
fig.update\u traces(marker\u sizeref=.025)
如果最小的标记太小,无法容纳文本。