[英]How to add customdata to a plotly Scattermapbox plot
我在帶有 go.Scattermapbox 的go.Scattermapbox
中顯示不同的氣候測量站。 現在我想通過在hovertemplate
命令中使用customdata
在 hover 中包含多個不同的值。 這適用於作為圖形一部分的值(如 lat、lon),但我無法弄清楚如何使customdata
命令與外部數據一起工作(例如,在 hover 框中添加高度值)。
plot_data = [
go.Scattermapbox(
lon=data_slctd["longitude"],
lat=data_slctd["latitude"],
mode="markers",
text = data_slctd.site_name.tolist(),
hovertemplate = "<b>%{text}</b><br><br>" + "longitude: %{lon:.2f}<br>" + "latitude: %{lat:.2f}<br>" + "altitude: %{customdata[0]:.0f}<br>"+ "ppm: %{marker.color:.2f}<extra></extra>",
)
]
As explained in the tutorial https://plotly.com/python-api-reference/generated/plotly.graph_objects.Scattermapbox.html (under "Adding other data to the hover with customdata and a hovertemplate") I have tried to include the customdata == np.dstack((hover_1, hover_2))
go.Scattermapbox
,但嘗試使用%customda[0}:.0f
訪問 hover_1 的第一個值不起作用。
hover_1 和 hover_2 保存顯示的所有站點的海拔高度值(即每個站點的長度與站點數量相同)。
這就是 hover 的樣子:
可能customdata
沒有顯示,因為customdata
數組的長度與lat
和lon
變量的長度不匹配。 當您執行customdata[0]
時,您試圖獲取customdata
數組的子列表的第一個元素。 例如,如果您有
x = [1,2,3],
y = [3,4,5]
在 plotly 圖中, customdata
應該類似於:
customdata = [
["a", "b"],
["c", "d"],
["e", "f"],
]
這樣,當您在(1,3)
上執行customdata[0]
和 hover 時,您將a
. 如果您在2,4
上執行 hover 並執行customdata[1]
您將得到d
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.