【问题标题】:Plotly Scattermapbox:有没有办法在标记上方和下方包含一些文本?
【发布时间】:2020-09-26 02:53:31
【问题描述】:

在 Plotly 中,使用 Scattermapbox,有没有办法在标记上方和下方显示一些文本?

目前文本仅在我将鼠标悬停在标记上时出现,并且绘图仅显示我想要显示的部分文本。

我的输入数据框df_area如下。我想显示包含在name 列和forecast 列中的文本。

     name   latitude   longitude    forecast
 0   "AK"   2.675000   203.139000   "Cloudy"
 1   "Bd"   2.621000   203.224000   "Cloudy"

但是,我目前只能在forecast 列中显示文本。

fig = go.Figure(go.Scattermapbox(
        lat=df_area["latitude"],
        lon=df_area["longitude"],
        mode="markers+text",
        marker={"size": 10},
        text=df_area["forecast"]))

【问题讨论】:

    标签: python plotly mapbox


    【解决方案1】:

    我在下面提供了一个示例,请注意,这需要(免费)mapbox 访问令牌。

    import plotly.graph_objects as go
    import pandas as pd
    
    mapbox_access_token = 'your-free-token'
    
    df = pd.DataFrame({'name': ['London', 'Oxford'],
                       'latitude': [51.509865, 51.7520],
                       'longitude': [-0.118092, -1.2577],
                       'forecast': ['Cloudy', 'Sunny']})
    
    data = go.Scattermapbox(lat=list(df['latitude']),
                            lon=list(df['longitude']),
                            mode='markers+text',
                            marker=dict(size=20, color='green'),
                            textposition='top right',
                            textfont=dict(size=16, color='black'),
                            text=[df['name'][i] + '<br>' + df['forecast'][i] for i in range(df.shape[0])])
    
    layout = dict(margin=dict(l=0, t=0, r=0, b=0, pad=0),
                  mapbox=dict(accesstoken=mapbox_access_token,
                              center=dict(lat=51.6, lon=-0.2),
                              style='light',
                              zoom=8))
    
    fig = go.Figure(data=data, layout=layout)
    

    【讨论】:

    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多