简体   繁体   English

Plotly:如何在 scattergeo plot 中自定义标签?

[英]Plotly: How to customize labels in a scattergeo plot?

In the code below I have marker labels which now is located close to these markers.在下面的代码中,我有标记标签,现在靠近这些标记。 What is the way to make customise destination between marker and it's label?在标记和它的 label 之间定制目的地的方法是什么? I want to put labels a little bit far from markers now.我现在想把标签放在离标记有点远的地方。

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

I see you're using a second Scattergeo trace to display your labels.我看到您正在使用第二个Scattergeo跟踪来显示您的标签。 And this does not seem to be a bad idea since it seems to me that fig.add_annotation can be bit tricky in a px.scatter_geo figure.这似乎不是一个坏主意,因为在我看来fig.add_annotationpx.scatter_geo图中可能有点棘手。 So I would simply adjust the latitude and longitude numbers slightly to get the labels in the positions you prefer, like lon=[float(d) + 10 for d in df['longitude']] :因此,我只需稍微调整纬度和经度数字即可将标签放在您喜欢的位置,例如lon=[float(d) + 10 for d in df['longitude']]

在此处输入图像描述

Complete code:完整代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM