简体   繁体   English

如何将自定义数据添加到 plotly Scattermapbox plot

[英]How to add customdata to a plotly Scattermapbox plot

I am displaying different climatic measuring stations in a map with go.Scattermapbox .我在带有 go.Scattermapbox 的go.Scattermapbox中显示不同的气候测量站。 Now I would like to include multiple different values in the hover by using customdata in the hovertemplate command.现在我想通过在hovertemplate命令中使用customdata在 hover 中包含多个不同的值。 This works fine for values that are part of the graph (like lat, lon) but I can not figure out how to make the customdata command work with external data (eg adding values for altitude in the hover box).这适用于作为图形一部分的值(如 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)) command within the go.Scattermapbox but trying to access the first values for hover_1 in the hovertemplate with %customda[0}:.0f did not work. 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 and hover_2 hold the values for altitude respective measuring methods for all stations displayed (ie each has the same length as the number of stations). hover_1 和 hover_2 保存显示的所有站点的海拔高度值(即每个站点的长度与站点数量相同)。

That's what the hover looks like:这就是 hover 的样子:

Screenshot of exemplary hover示例 hover 的屏幕截图

Maybe the customdata was not displayed because the length of the customdata array did not match with the length of the lat and lon variables.可能customdata没有显示,因为customdata数组的长度与latlon变量的长度不匹配。 When you do customdata[0] , you are trying to get the first element of the sublist of the customdata array.当您执行customdata[0]时,您试图获取customdata数组的子列表的第一个元素。 For example, if you have例如,如果您有

x = [1,2,3],
y = [3,4,5]

in a plotly figure, then customdata should be something like:在 plotly 图中, customdata应该类似于:

customdata = [
  ["a", "b"],
  ["c", "d"],
  ["e", "f"],
]

That way when you do customdata[0] and hover on (1,3) you will get a .这样,当您在(1,3)上执行customdata[0]和 hover 时,您将a . If you hover on 2,4 and do customdata[1] you will get d .如果您在2,4上执行 hover 并执行customdata[1]您将得到d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM