在 plotly 中使用不同类型的子图时悬停无法正常工作

问题描述 投票:0回答:1

我正在尝试生成一个包含一个 Scattermapbox 和多个其他类型的图(在本例中是使用 Scatter 生成的线图)的绘图图。一切看起来都不错,但是当我将鼠标悬停在线条图上时,没有显示任何悬停信息。

如果我不创建 Scattermapbox-plot,将鼠标悬停在线图上可以正常工作(在下面的最小示例中,您可以通过取消注释

fig.show()
exit()
线来测试它)。

我做错了什么和/或有什么办法可以解决这个问题吗?

import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go


fig = make_subplots(
    rows=2, cols=2,
    specs=[
        [{'rowspan':2, 'type' :'mapbox'},{'type' : 'scatter'}],
        [None,{'type' : 'scatter'}],
    ],
    print_grid=True,
)

x = np.linspace(0,2*np.pi,100)
y1 = np.sin(x)
y2 = np.cos(x)

fig.add_trace(
    go.Scatter(
        x=x,
        y=y1,
    ),
    row=1, col=2,
)


fig.add_trace(
    go.Scatter(
        x=x,
        y=y2,
    ),
    row=2, col=2,
)

# UNCOMMENT THESE TWO LINES TO TEST WITHOUT SCATTERMAPBOX
#fig.show()
#exit()

coordinates = {
    'Berlin' : (52.5200, 13.4050),
    'Munich' : (48.1351, 11.5820),
    'Hamburg' : (53.5488, 9.9872),
    'Frankfurt' : (50.1109, 8.6821),
    'Dresden' : (51.0504, 13.7373),
}

z, lat, lon, names = zip(*[
    (i,v[0],v[1],k) for i,(k,v) in enumerate(coordinates.items())])
    
fig.add_trace(
    go.Scattermapbox(
        lat = lat,
        lon = lon,
        mode = 'markers+text',
        text = names,
        below = '',
        marker_size=12,
        marker_color=z,
        name = 'town',
    ),
    col=1, row=1,
)

fig.update_layout(margin=dict(l=20, r=0, t=40, b=40))
    
fig.update_layout(
    mapbox1=dict(
        zoom=4, style='carto-positron',
        center={k:v for k,v in zip(['lat','lon'],coordinates['Frankfurt'])}, 
    ),
)

fig.show()
python plotly
1个回答
0
投票

由于scattertermapbox使用的是mapbox提供的图层,我想我需要指定可以提供的图层名称和一个免费的API token。有关详细信息,请参阅参考资料。以下代码将创建一个包含地图的子图。

import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go

mapbox_access_token = open("./mapbox_api_key.txt").read()

fig = make_subplots(
    rows=2, cols=2,
    specs=[
        [{'rowspan':2, 'type' :'mapbox'},{'type' : 'scatter'}],
        [None,{'type' : 'scatter'}],
    ],
    print_grid=True,
)

x = np.linspace(0,2*np.pi,100)
y1 = np.sin(x)
y2 = np.cos(x)

fig.add_trace(go.Scatter(x=x, y=y1,), row=1, col=2,)


fig.add_trace(go.Scatter(x=x, y=y2,), row=2, col=2,)

coordinates = {
    'Berlin' : (52.5200, 13.4050),
    'Munich' : (48.1351, 11.5820),
    'Hamburg' : (53.5488, 9.9872),
    'Frankfurt' : (50.1109, 8.6821),
    'Dresden' : (51.0504, 13.7373),
}

z, lat, lon, names = zip(*[
    (i,v[0],v[1],k) for i,(k,v) in enumerate(coordinates.items())])
    
fig.add_trace(
    go.Scattermapbox(
        lat = lat,
        lon = lon,
        mode = 'markers+text',
        text = names,
        below = '',
        marker_size=12,
        marker_color=z,
        name = 'town',
    ),
    col=1, row=1,
)

fig.update_layout(margin=dict(l=20, r=0, t=40, b=40))
    
fig.update_layout(
    mapbox1=dict(
        zoom=4,
        style='light',
        accesstoken=mapbox_access_token,
        center={k:v for k,v in zip(['lat','lon'],coordinates['Frankfurt'])}, 
    ),
)

fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.