Python_Calculative Plotly 库中标签的坐标
Python_Calculative coordinates for labels in Plotly lib
在下面的代码中,我在地图本身上有标记的标签。
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)
fig.add_trace(go.Scattergeo(lon=df["longitude"],
lat=df["latitude"],
text = tag,
textposition="middle right",
mode='text',
showlegend=False))
或使用texttemplate
:
fig.add_trace(go.Scattergeo(lon=df["longitude"],
lat=df["latitude"],
text=df["data"],
textposition="middle right",
mode='text',
showlegend=False,
texttemplate=" %{text}"
))
更新:可以使用 texttemplate
通过指定基于 df.data
的可变宽度并使用右对齐来进一步格式化,请参阅 Format Specification Mini-Language 了解更多选项。类似于:
#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
))
原文:
缩放:
在下面的代码中,我在地图本身上有标记的标签。
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)
fig.add_trace(go.Scattergeo(lon=df["longitude"],
lat=df["latitude"],
text = tag,
textposition="middle right",
mode='text',
showlegend=False))
或使用texttemplate
:
fig.add_trace(go.Scattergeo(lon=df["longitude"],
lat=df["latitude"],
text=df["data"],
textposition="middle right",
mode='text',
showlegend=False,
texttemplate=" %{text}"
))
更新:可以使用 texttemplate
通过指定基于 df.data
的可变宽度并使用右对齐来进一步格式化,请参阅 Format Specification Mini-Language 了解更多选项。类似于:
#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
))
原文:
缩放: