本文转载请联系python和大数据分析公众号。我已经两周没更新了公众号了。首先,我工作有点忙。其次,我对地图太困惑了。事情还没搞清楚就写文档是对我最大的不尊重。choropleth_mapboxmap的实现有很多坑在里面。主要是对geojson的了解不够,一直以来choropleth_mapbox对参数的解释不清楚。GeoJSON是一种用于编码各种地理数据结构的格式。GeoJSON以json语法表达和存储地理数据,可以说是json的一个子集。GeoJSON对象可以表示几何、特征或特征集合。GeoJSON支持以下几何类型:点、线、多边形、多点、多线、多多边形和几何集合。GeoJSON中的要素包含几何对象和其他属性,要素集合表示一组要素。GeoJSON始终由单个对象组成。此对象表示几何、特征或特征集合。GeoJSON对象可以有任意数量的成员。GeoJSON对象必须有一个名为“type”的成员。此成员的值是由GeoJSON对象的类型确定的字符串。类型成员的值必须是以下之一:“Point”、“MultiPoint”、“LineString”、“MultiLineString”、“Polygon”、“MultiPolygon”、“GeometryCollection”、“Feature”或“FeatureCollection”。以下是geojosn的示例{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[105.380859375,31.57853542647338]}}]}全球地图、中国地图、省份地图的geojson文件都可以下载,只是格式略有不同。比如全球地图有id,是国家的缩写,properties下的name也有全名。但是中国地图有adcode、name、level、centerpoint等属性。在执行choropleth_mapbox的过程中,地图一直无法正常显示。有两个原因。一是plotly是基于d3.js的,加载geojson文件比较耗时,需要点击缩小按钮才能显示地图。第二个参数错误。下面代码注释中已经介绍过,这里不再赘述。所有数据均为随机值,不代表任何意义。importjsonimportpandasaspdimportplotly.expressaspxdefprint_json(data):print(json.dumps(data,sort_keys=True,indent=4,separators=(',',':'),ensure_ascii=False))withopen('countries.geo.json')asresponse:counties=json.load(response)df=pd.read_csv("datarand.csv",encoding="utf-8")#世界地图,不指定key值,默认使用geojson中的id值,即即国家缩写,数据表中的列也应该是国家缩写,即国家列fig=px.choropleth_mapbox(df,geojson=counties,locations='country',color='key1',color_continuous_scale=px.colors.diverging.RdYlGn[::-1],range_color=(100,10000),mapbox_style="carto-positron",zoom=1,center={"lat":37.4189,"lon":116.4219},opacity=0.5)fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})fig.show()#世界地图,指定properties.namecountryname为键值,数据表中的列也要改为国家,即locations列fig=px.choropleth_mapbox(df,geojson=counties,featureidkey="properties.name",locations='name',color='key1',color_continuous_scale="Viridis",range_color=(100,10000),mapbox_style="carto-positron",zoom=1,center={"lat":37.4189,"lon":116.4219},opacity=0.5)fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})fig.show()#世界地图,指定id国家缩写为key值,数据表中的列也要改为国家缩写,即国家列fig=px.choropleth_mapbox(df,geojson=counties,featureidkey="id",locations='country',color='key1',color_continuous_scale="Reds",range_color=(100,10000),mapbox_style="carto-positron",zoom=1,center={"lat":37.4189,"lon":116.4219},opacity=0.5)fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})fig.show()#世界地图,不指定key值,默认使用geojson中的id值,即国家的缩写,数据表中的列也应该是国家的缩写,即国家列。setcolor_continuous_scalefig=px.choropleth_mapbox(df,geojson=counties,locations='country',color='key1',range_color=(100,10000),color_continuous_scale=[[0,'lightcoral'],#这个一定要写,否则会报错[1./3000,'indianred'],[1./300,'brown'],[1./30,'firebrick'],[1/3,'maroon'],[1.,'darkred']],zoom=1,center={"lat":37.4189,"lon":116.4219},mapbox_style='carto-positron')图。update_layout(margin={"r":0,"t":0,"l":0,"b":0})fig.show()#Chinesemapwithopen('china_geo.json')asresponse:counties=json.load(response)df=pd.read_csv("data.csv",encoding="utf-8",dtype={"areacode":str})fig=px.choropleth_mapbox(df,geojson=counties,featureidkey="properties.adcode",locations='areacode',color='confirm',#color_continuous_scale="Viridis",range_color=(1,80000),color_continuous_scale=[[0,'lightcoral'],#这个必须写,否则会报错[1./3000,'indianred'],[1./300,'brown'],[1./30,'firebrick'],[1/3,'maroon'],[1.,'darkred']],zoom=3,center={"lat":37.4189,"lon":116.4219},mapbox_style='carto-positron')fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})fig.show()#海南地图withopen('460000-hainan.json')asresponse:counties=json.load(response)df=pd.read_csv("hainandata.csv",encoding="utf-8",dtype={"areacode":str})fig=px.choropleth_mapbox(df,geojson=县,featureidkey="properties.adcode",locations='areacode',color='confirm',#color_continuous_scale="Geyser",color_continuous_scale='Reds',#color_continuous_scale=px.colors.diverging.RdYlGn[::-1],range_color=(1,1500),zoom=6,center={"lat":20.031971,"lon":110.33119},mapbox_style='carto-positron')fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})fig.show()
