简体   繁体   中英

Python_Calculative coordinates for labels in Plotly lib

In the code below I have marker's labels on the map itself.

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

I put this piece of code:

float(d) + 5 for d in df["longitude"]],
lat=[float(d) + 0 for d in df["latitude"]]

to make these labels close to the markers. 在此处输入图像描述 But in case of resizing of map these labels looks weird. 标记和标签之间的长度太大。 I believe that it is possible to put into condition of labels position not only absolute values as it is now, but some sort of dependency between labels coordinates and values in data.

One method that might work for you is padding your df.data values with spaces. For example here the data is padded with three spaces in the new variable tag .

# tag = ['   15', '   4', '   41']
tag = "   " + df['data'].astype(str)

fig.add_trace(go.Scattergeo(lon=df["longitude"],
              lat=df["latitude"],
              text = tag,
              textposition="middle right",
              mode='text',
              showlegend=False))

or using texttemplate :

fig.add_trace(go.Scattergeo(lon=df["longitude"],
              lat=df["latitude"],
              text=df["data"],
              textposition="middle right",
              mode='text',
              showlegend=False,
              texttemplate="   %{text}"
                           ))

UPDATE: further formatting can be done with texttemplate by specifying a variable width based on df.data and using right justification, see Format Specification Mini-Language for more options. Something like:

#text template width based on formula width=x/12+3 and '>' to right justify
tt = ["%{text:>" + str(x/12 + 3) + "}" for x in df['data']]

fig.add_trace(go.Scattergeo(lon=df["longitude"],
              lat=df["latitude"],
              text=df["data"],
              textposition="middle right",
              mode='text',
              showlegend=False,
              texttemplate= tt
                           ))

original:

在此处输入图像描述

zoomed:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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