Python plotly_Connection 标记与线内的标签 [英] Python plotly_Connection markers with labels within line

查看:22
本文介绍了Python plotly_Connection 标记与线内的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我有标记标签.现在我想制作将标记中心与文本连接起来的线条(见下图).

In the code below I have marker labels. Now I want to make lines which connect center of markers with text (see pic below).

制作方法是什么?

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=df["longitude"],
              lat=df["latitude"],
              text=df["data"],
              textposition="middle right",
              mode='text',
              showlegend=False))
fig.show()

如果您将经度增加 10,则会出现反映地球上平行线的弧线.在大规模或当您在代码中添加更大的数字时,它非常明显.

In case you added 10 to the longitude, there appered arcs reflecting parallels on the globe. It's very visible on big scale or whent you add bigger figures in code.

推荐答案

annotations options 我不知道如何在地图中使用 update_layout.我想到的是在距离点的一定距离处添加标签(在这里,我在经度上添加了 10),然后手动添加线.见下文;

Looking at the annotations options I couldn't figure out how to use update_layout within the maps. What I think of was adding the labels at some distance (here, I added 10 to the longitude) from the points and then adding the lines manually. See below;

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})
df = df.astype({"longitude": float})
df = df.astype({"latitude": float})

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=df["longitude"]+10,
              lat=df["latitude"],
              text=df["data"],
              textposition="middle right",
              mode='text',
              showlegend=False))

for i in range(len(df)):
    fig.add_trace(
        go.Scattergeo(
            lon = [df['longitude'][i], df['longitude'][i]+9],
            lat = [df['latitude'][i], df['latitude'][i]],
            mode = 'lines',
            line = dict(width = 1,color = 'red'),
            showlegend=False
        )
    )


fig.show()

这篇关于Python plotly_Connection 标记与线内的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆