【问题标题】:Python - plotly - combine bubble and choropleth mapPython - plotly - 结合气泡和等值线图
【发布时间】:2015-08-26 12:52:47
【问题描述】:

R - plotly - combine bubble and chorpleth map这个问题的答案中,很好地描述了如何在一张地图中结合气泡图和等值线图。我想在 Python 中复制完全相同的示例,但是我没有根据 plotly 文档 (https://plot.ly/python/reference/) 中提供的信息进行管理。

等价的R代码如下:

lon = c(-73.9865812, -118.2427266, -87.6244212, -95.3676974)
pop = c(8287238, 3826423, 2705627, 2129784)
df_cities = data.frame(cities, lat, lon, pop)

state_codes = c("NY", "CA", "IL", "TX")
pop = c(19746227.0, 38802500.0, 12880580.0, 26956958.0)
df_states = data.frame(state_codes, pop)

plot_ly(df_cities, lon=lon, lat=lat, 
        text=paste0(df_cities$cities,'<br>Population: ', df_cities$pop), 
        marker= list(size = sqrt(pop/10000) + 1), type="scattergeo",
        filename="stackoverflow/choropleth+scattergeo") %>%
  add_trace(z=df_states$pop,
            locations=df_states$state_codes, 
            text=paste0(df_states$state_codes, '<br>Population: ', df_states$pop),
            type="choropleth", 
            colors = 'Purples', 
            locationmode="USA-states") %>%
  layout(geo = list(scope="usa"))

这如何在 Python 中实现?

【问题讨论】:

    标签: python dictionary plotly


    【解决方案1】:

    只需将.py 附加到图形的URL(例如https://plot.ly/~RPlotBot/1735.py)即可查看相应的python 代码。

    【讨论】:

      【解决方案2】:

      我发现我必须稍微调整一下代码才能让它工作:

      cities = c('New York', 'Los Angeles', 'Chicago', 'Houston')
      lat = c(40.7305991, 34.053717, 41.8755546, 29.7589382)
      lon = c(-73.9865812, -118.2427266, -87.6244212, -95.3676974)
      pop = c(8287238, 3826423, 2705627, 2129784)
      df_cities = data.frame(cities, lat, lon, pop)
      
      state_codes = c("NY", "CA", "IL", "TX")
      pop = c(19746227.0, 38802500.0, 12880580.0, 26956958.0)
      df_states = data.frame(state_codes, pop)
      
      plot_ly(df_cities, lon=lon, lat=lat,
               text = paste0(df_cities$cities,'<br>Population: ', df_cities$pop),
               marker = list(size = sqrt(pop/10000) + 1), type="scattergeo",
               filename = "stackoverflow/choropleth+scattergeo") %>%
       add_trace(z=df_states$pop,
          locations = df_states$state_codes,
          text = paste0(df_states$state_codes, '<br>Population: ', df_states$pop),
          type = "choropleth",
          colors = 'Purples',
          locationmode = "USA-states") %>%
       layout(geo = list(scope="usa"))
      

      【讨论】:

      • 这不是问题所要求的 python 答案
      猜你喜欢
      • 2015-11-09
      • 1970-01-01
      • 2021-11-30
      • 2023-04-08
      • 2018-03-09
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2023-01-29
      相关资源
      最近更新 更多