Plotly:基于 GeoJSON 文件创建地图

Plotly: Create map based on GeoJSON file

我找到了包含荷兰所有城市的 GeoJSON file。我现在正尝试使用 Plotly 绘制这张地图,但我似乎无法弄清楚该怎么做。我知道可以在特定国家/地区设置范围,但 Plotly 的默认地图似乎不包含每个国家/地区的各个城市。

我的工作基于 this JSFiddle I would elsewhere on Whosebug. I know the data is not relevant for The Netherlands, but that doesn't matter at this moment: all I want is a map that looks somewhat like this(但我喜欢在 Plotly 中进行,因为 Python 支持 Plotly)。

非常感谢任何帮助!

Plotly.d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv', function(err, rows){
      function unpack(rows, key) {
          return rows.map(function(row) { return row[key]; });
      }

 var data = [{
              type: 'choropleth',
              locations: unpack(rows, 'CODE'),
              z: unpack(rows, 'GDP (BILLIONS)'),
              text: unpack(rows, 'COUNTRY'),
              colorscale: [[0,'rgb(5, 10, 172)'],[0.35,'rgb(40, 60, 190)'],[0.5,'rgb(70, 100, 245)'], [0.6,'rgb(90, 120, 245)'],[0.7,'rgb(106, 137, 247)'],[1,'rgb(220, 220, 220)']],
              autocolorscale: false,
              reversescale: true,
              marker: {
                line: {
                  color: 'rgb(180,180,180)',
                  width: 0.5
                }
              },
              tick0: 0,
              zmin: 0,
              dtick: 1000,
              colorbar: {
                autotic: false,
                tickprefix: '$',
                title: 'GDP<br>Billions US$'
              }
          }];

console.log(data.locations);
  var layout = {
          title: '2014 Global GDP<br>Source: <a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html"> CIA World Factbook</a>',
          geo:{
            showframe: false,
            showcoastlines: false,
            projection:{
              type: 'mercator'
            }
          }
      };
      Plotly.plot(myDiv, data, layout, {showLink: false});
  });

在plotly中,可以使用scope来限制显示的底图。不幸的是,开箱即用,此处仅列出了几个范围:https://plot.ly/python/reference/#layout-geo-scope

为了限制显示为底图的内容,有一个解决方法,它使用 lonaxis 和 lataxis,如下所示:http://codepen.io/etpinard/pen/XKamdk. This issue is documented here for reference: https://github.com/plotly/plotly.js/issues/719

要从 geoJSON 获取点,只需将 geoJSON 中的坐标用作数据变量中的 lon 和 lat。此外,如果您尝试绘制城市散点图,则应使用数据 type: 'scattergeo' 而不是 type: 'choropleth'。这是来自 plotly 的教程的 link:https://plot.ly/javascript/scatter-plots-on-maps/.