【问题标题】:Adding annotations to Plotly Scatter Map向 Plotly 散点图添加注释
【发布时间】:2016-03-23 20:33:32
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python pandas plotly


    【解决方案1】:

    你可以试试类似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()
    

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

    【讨论】:

    • 我希望在绘图而不是常规网格上绘制注释。编辑了我的问题并添加了图片以尝试使其更清晰。
    • @fishball,抱歉我没注意到。也许您可以提供一些示例数据集,以便那些愿意提供帮助的人尝试一下(测试一下)?
    猜你喜欢
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2022-01-20
    • 2020-02-25
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多