【问题标题】:Clusters does not appear on my map with scatter Mapbox集群没有出现在我的地图上,带有 scatter Mapbox
【发布时间】:2021-10-26 20:47:39
【问题描述】:

我在使用 Plotly 时遇到了一个关于我的 scatter Mapbox 的问题。虽然我的代码没问题(我在 Jupiter notebook 上工作),但没有出现集群(而当我用计算机鼠标飞过时标签出现......):

fig = px.scatter_mapbox(df2, lat="Lat", lon="Lon", zoom=8, color = "Cluster_KMeans", mapbox_style="carto-positron")
fig.show()

导入的库

! pip install plotly
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.io as pio
pio.renderers.default = "iframe_connected"

非常感谢您的帮助!

【问题讨论】:

  • 您的代码不是reproducible,因为不知道df2 的样子,我们无法重现您的问题。您能否发布您的 DataFrame 示例以及屏幕截图以更清楚地解释您的问题?谢谢

标签: plotly mapbox scatter


【解决方案1】:
  • 您尚未在问题中定义df2。我可以暗示它在您的代码中有三列:Lat、Lon、Cluster_KMeans。因此已经找到了一些位置并添加了一列随机值
  • 使用您的代码生成的图形。它按预期工作。
import plotly.express as px
import pandas as pd
import numpy as np

df2 = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0].rename(columns={"Latitude":"Lat", "Longitude":"Lon"})
df2["Cluster_KMeans"] = np.random.randint(1,5,len(df2))
fig = px.scatter_mapbox(df2, lat="Lat", lon="Lon", zoom=8, color = "Cluster_KMeans", mapbox_style="carto-positron")
fig.show()

【讨论】:

  • 您好 Rob,感谢您的回复,但不起作用。我确实有 3 列,并且 df2 定义明确。我会再次检查
  • 如果您具体说明什么不起作用,我可以查看 不起作用。 1)由于异常而无法构建图形? 2)图形建立但空白? 3)悬停数据不是你所期望的? 4) 配色方案不符合您的要求 5) 笔记本电脑关机 :-) 等...需要继续进行
  • 你好 Rob,我切换到 Google Colab 而不是 Jupyter 并且它运行了
  • 非常感谢您的帮助 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 2018-07-14
  • 2020-03-09
  • 1970-01-01
  • 2014-07-18
相关资源
最近更新 更多