gpt4 book ai didi

python - 向 Plotly 散点图添加注释

转载 作者:行者123 更新时间:2023-11-28 19:13:49 27 4
gpt4 key购买 nike

我在向我的 plotly scattergeo map 添加注释时遇到问题。我使用 pandas 数据框作为我的数据源,其中包含 Lat、Long 列,文本是索引。我生成的嵌套在布局参数下的注释参数如下所示:

[dict(x= d[1]['Lat'], y= d[1]['Long'], text= d[0]) for d in df.iterrows()]

相反,我可以只使用一行的单个注释(或者为了测试目的而硬编码值)。似乎 x,y 放置是使用图表网格而不是 map 网格来放置注释。是否有一个参数可以解决这个问题,或者我是否需要调整图表网格本身?提前谢谢你。

编辑:这是一个例子。通过将纬度/经度坐标作为注释的 x、y 变量传递,我希望注释位于每个气泡的旁边。

plotly map example

最佳答案

您可以尝试类似于 that 的操作:

import plotly.plotly as py
import plotly.graph_objs as go

fig = go.Figure(
data=[
go.Scattergeo(
lat=[45.5,43.4,49.13,51.1,53.34,45.24,44.64,48.25,49.89,50.45],
lon=[-73.57,-79.24,-123.06,-114.1,-113.28,-75.43,-63.57,-123.21,-97.13,-104.6],
marker={
"color": ["#bebada","#fdb462","#fb8072","#d9d9d9","#bc80bd","#b3de69","#8dd3c7","#80b1d3","#fccde5","#ffffb3"],
"line": {
"width": 1
},
"size": 10
},
mode="markers+text",
name="",
text=["Montreal","Toronto","Vancouver","Calgary","Edmonton","Ottawa","Halifax","Victoria","Winnepeg","Regina"],
textfont={
"color": ["#bebada","#fdb462","#fb8072","#d9d9d9","#bc80bd","#b3de69","#8dd3c7","#80b1d3","#fccde5","#ffffb3"],
"family": ["Arial, sans-serif","Balto, sans-serif","Courier New, monospace","Droid Sans, sans-serif","Droid Serif, serif","Droid Sans Mono, sans-serif","Gravitas One, cursive","Old Standard TT, serif","Open Sans, sans-serif","PT Sans Narrow, sans-serif","Raleway, sans-serif","Times New Roman, Times, serif"],
"size": [22,21,20,19,18,17,16,15,14,13]
},
textposition=["top center","middle left","top center","bottom center","top right","middle left","bottom right","bottom left","top right","top right"]
)
],
layout={
"title": "Canadian cities",
"geo": {
"lataxis": {
"range": [40, 70]
},
"lonaxis": {
"range": [-130, -55]
},
"scope": "north america"
}
}
)

plot_url = py.plot(fig, filename='Canadian Cities')

旧答案:

import pandas as pd

# initial position
lat = 47.8388
lon = 35.1396

# generate random coordinates
df = pd.DataFrame({'lat': lat + np.random.normal(1,0.2,10),'lon': lon + np.random.normal(1,0.2,10)})
df = df.round(4)

pd.options.display.float_format = '{:.4f}'.format
df['text'] = df.lat.astype(str) + ', ' + df.lon.astype(str)
print(df)

ax = df.plot(x='lat', y='lon', kind='scatter')

y_shift = (df.lon.max()-df.lon.min())/len(df)
[ax.annotate(tup[2], xy=tup[:2], xytext=(tup[0], tup[1]))
for tup in df.itertuples(index=False)]

plt.show()

我把坐标作为注释的文本...

Plot

关于python - 向 Plotly 散点图添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36188179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com