简体   繁体   English

Python 来自 Geopandas Dataframe 的 Chloropleth Mapbox

[英]Python Chloropleth Mapbox from Geopandas Dataframe

I have a geopandas dataframe df which looks like this:我有一个 geopandas dataframe df ,它看起来像这样:

数据框

The st_astext column is set to the dataframe geometry. st_astext列设置为 dataframe 几何。 I'm trying to plot this as a plotly mapbox chloropleth map with the color proportional to cnt for polygons in st_astext , using我正在尝试将 plot 用作 plotly mapbox chloropleth map,颜色与st_astext中的多边形的cnt成比例,使用

trcs = [px.choropleth_mapbox(df, geojson=df.geometry, locations=df['id'], color='cnt', color_continuous_scale="Viridis")]
lout = go.Layout(title='ch', mapbox_accesstoken=mapToken, width=1600, height=1200, mapbox={'style':'streets', 'zoom':10.5,'center':{'lat':boundingBox['centroid_lat'].values[0], 'lon':boundingBox['centroid_lon'].values[0]}})
plyoff.iplot(go.Figure(data=trcs, Layout=lout))

This gives me this partial stack trace:这给了我这个部分堆栈跟踪:

ValueError: 
    Invalid element(s) received for the 'data' property of 
        Invalid elements include: [Figure({
    'data': [{'coloraxis': 'coloraxis',
              'geojson': {'bbox': (-118.28177341, 33.7155889101773,
                                   -118.18514585495, 33.7780896175267),
                          'features': [{'bbox': (-118.279454401, 33.7495355583513,
                                                 -118.271920681, 33.756297219837),
                                        'geometry': {'coordinates': (((-118.279454
                                                                     401,
                                                                     33.754490905),
                                                                     (-118.27537536
                                                                     6211, 33.74953
                                                                     55583513), (-1
                                                                     18.271920681, 
                                                                     33.75105207235
                                                                     19), (-118.275
                                                                     847434998, 33.
                                                                     756297219837),
                                                                     (-118.27945440
                                                                     1, 33.75449090
                                                                     5))),
                                                     'type': 'Polygon'},
                                        'id': '0',
                                        'properties': {},
                                        'type': 'Feature'},...

Any ideas?有任何想法吗? I may be going at this completely wrong, but the documentation for such plotting seems to leave a lot to be desired.我可能完全错了,但是这种绘图的文档似乎还有很多不足之处。 I've tried various permutations, with no luck.我尝试了各种排列,但没有运气。

  • you have not provided sample data.你没有提供样本数据。 Have generated 10 polygons to demonstrate已生成 10 个多边形来演示
  • the screen shot of your data shows a pandas dataframe with a column st_astext which is well know text https://shapely.readthedocs.io/en/stable/manual.html#well-known-formats您的数据的屏幕截图显示了pandas dataframe 列st_astext是众所周知的文本Z5E056C500A1C4B6A7110B50D807BADE-format5Z://shapely.readthedocs#-wellknown-format5Z://shapely.en.readthedocs#-wellknown-format5Z
  • an approach to using this with plotly将其与 plotly 一起使用的方法
    1. decode WKT to shapely using shapely.wkt.loads()使用shapely.wkt.loads()WKT解码为匀称
    2. create a geopandas GeoSeries from this pandas Series .从此pandas Series创建一个geopandas GeoSeries This then has the interface __geo_interface__ to generate GEOJSON然后有接口__geo_interface__来生成GEOJSON
  • "with the color proportional to cnt for polygons" I'm assuming means number of points in a polygon “颜色与多边形的cnt成比例”我假设是指多边形中的点数
import pandas as pd
import io
import geopandas as gpd
import shapely.geometry
import plotly.express as px

# fmt: off
df = pd.read_csv(io.StringIO('''st_astext
"POLYGON ((-3.86 50.67, -3.75 50.64, -3.46 50.58, -3.47 50.58, -3.47 50.56, -3.49 50.54, -3.51 50.52, -3.5 50.52, -3.47 50.46, -3.46 50.4, -3.51 50.38, -3.52 50.35, -3.54 50.34, -3.57 50.35, -3.58 50.38, -3.59 50.38, -3.58 50.37, -3.58 50.36, -3.56 50.34, -3.57 50.33, -3.61 50.32, -3.64 50.29, -3.66 50.24, -3.64 50.22, -3.68 50.22, -3.72 50.2, -3.74 50.21, -3.77 50.22, -3.76 50.24, -3.75 50.24, -3.74 50.24, -3.76 50.24, -3.75 50.26, -3.76 50.25, -3.77 50.26, -3.76 50.24, -3.78 50.23, -3.78 50.21, -3.81 50.21, -3.86 50.67))"
"POLYGON ((-3.12 53.56, -3.12 53.55, -3.12 53.55, -3.11 53.56, -3.04 53.53, -3 53.44, -3 53.42, -3.18 53.32, -3.22 53.34, -3.2 53.35, -3.23 53.37, -3.26 53.45, -3.16 53.43, -3.1 53.43, -3.12 53.43, -3.11 53.44, -3.11 53.45, -3.1 53.45, -3.09 53.44, -3.07 53.45, -3.09 53.45, -3.09 53.45, -3.06 53.46, -3.05 53.45, -3.06 53.45, -3.03 53.45, -3.03 53.45, -3.02 53.45, -3.07 53.49, -3.09 53.52, -3.15 53.54, -3.11 53.54, -3.12 53.54, -3.12 53.55, -3.12 53.56, -3.12 53.56))"
"POLYGON ((-3.63 51.13, -3.36 50.83, -3.37 50.76, -3.75 50.64, -3.86 50.67, -3.89 50.74, -3.63 51.13))"
"POLYGON ((-1.87 50.87, -1.67 50.8, -1.64 50.73, -1.69 50.74, -1.73 50.73, -1.75 50.71, -1.82 50.72, -1.9 50.71, -1.95 50.68, -1.94 50.67, -1.95 50.65, -1.92 50.64, -1.95 50.62, -1.96 50.61, -1.94 50.61, -1.96 50.59, -2.01 50.59, -1.87 50.87))"
"POLYGON ((-3.63 51.13, -3.61 51.22, -3.58 51.23, -3.54 51.23, -3.5 51.23, -3.47 51.21, -3.45 51.22, -3.4 51.19, -3.35 51.18, -3.28 51.18, -3.24 51.19, -3.12 50.82, -3.36 50.83, -3.63 51.13))"
"POLYGON ((-3.37 50.76, -3.36 50.83, -3.12 50.82, -2.91 50.75, -2.9 50.73, -2.92 50.73, -2.93 50.72, -3.02 50.7, -3.08 50.7, -3.1 50.69, -3.19 50.68, -3.25 50.67, -3.31 50.63, -3.35 50.62, -3.36 50.61, -3.38 50.6, -3.38 50.61, -3.37 50.76))"
"POLYGON ((-3.38 50.61, -3.43 50.62, -3.44 50.61, -3.43 50.63, -3.44 50.63, -3.43 50.63, -3.44 50.65, -3.44 50.63, -3.45 50.64, -3.44 50.65, -3.45 50.66, -3.44 50.62, -3.44 50.61, -3.42 50.61, -3.42 50.61, -3.46 50.58, -3.75 50.64, -3.37 50.76, -3.38 50.61))"
"POLYGON ((-2.34 50.99, -2.24 51.02, -2.22 51.01, -2.21 50.62, -2.24 50.62, -2.32 50.62, -2.34 50.63, -2.43 50.64, -2.45 50.61, -2.44 50.61, -2.45 50.6, -2.43 50.6, -2.45 50.6, -2.47 50.58, -2.46 50.57, -2.42 50.57, -2.43 50.57, -2.42 50.55, -2.46 50.51, -2.45 50.56, -2.54 50.62, -2.71 50.69, -2.34 50.99))"
"POLYGON ((-2.73 51.5, -2.71 51.31, -2.85 51.14, -2.86 51.14, -3.02 51.25, -3.01 51.24, -3.03 51.25, -3.03 51.25, -3.06 51.28, -3.04 51.29, -3.03 51.32, -3.04 51.33, -3.02 51.33, -2.98 51.38, -2.98 51.39, -2.91 51.4, -2.89 51.43, -3.11 51.34, -3.1 51.34, -3.12 51.34, -3.11 51.34, -3.12 51.38, -2.78 51.53, -2.73 51.53, -2.73 51.5))"
"POLYGON ((-2.86 51.14, -2.85 51.14, -2.34 50.99, -2.71 50.69, -2.74 50.7, -2.88 50.73, -2.9 50.73, -2.91 50.75, -2.86 51.14))"'''
                       ))
# fmt: on

px.choropleth_mapbox(
    geojson=gpd.GeoSeries(df["st_astext"].apply(shapely.wkt.loads)).__geo_interface__,
    locations=df.index,
    color=df["st_astext"]
    .apply(shapely.wkt.loads)
    .apply(lambda p: len(p.exterior.coords)),
).update_layout(
    mapbox={"style": "carto-positron", "center": {"lon": -2, "lat": 51}, "zoom": 5}
)

在此处输入图像描述

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

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