Python错误中的绘图图:初始化列表中没有参数

发布于 2025-02-09 18:12:14 字数 1952 浏览 1 评论 0 原文

从以下文件中,我想使用folium:

polygons文件:? 邮政编码可变级别:

from1(folium):

import pandas as pd
import geopandas as gpd
import folium

df_postcode_variable = pd.read_csv("postcode_sample.csv", dtype=str)  # Postcode variable level
df_postcode_variable['people'] = df_postcode_variable['people'].astype(float)
df_polygons = gpd.read_file("postcode_polygons.shp") # Polygons file
df_polygons.crs = "EPSG:4326"

my_map = folium.Map(location=[41.3890727,2.1572654], zoom_start=13,
                   detect_retina=True, control_scale=False)

folium.Choropleth(
        geo_data=df_polygons,
        name='choropleth',
        data=df_postcode_variable,
        columns=['cp','people'],
        key_on='cp',
        fill_color='OrRd',
        fill_opacity=0.5,
        line_opacity=0.8,
        line_color='Blue',
        legend_name='number of people'
    ).add_to(my_map)

my_map

但是我会收到以下错误:

ValueError: key_on `'cp'` not found in GeoJSON.

尝试2(pandas_bokeh)工作:

import pandas_bokeh
pandas_bokeh.output_notebook()

df_joined=df_polygons.merge(df_postcode_variable.set_index('cp'), on='cp')

df_joined.plot_bokeh(simplify_shapes=20000,
                  category="people", 
                  colormap="Spectral", 
                  hovertool_columns=["cp","people"])

“在此处输入图像说明”

我如何在forium上修复此错误? CP在这两个物体中都是如此,这对我来说没有意义。看来 df_polygons 中的 cp

From the following files I want to do a map plot by using folium:

Polygons file: https://drive.google.com/file/d/1uhj7OyHktPseR_CtRD0vPfq8HziHA4RK/view?
Postcode variable level: https://drive.google.com/file/d/1oFBITKzqLyTvQEASmZmO6CGCOzdPacgH/view?

Attempt1 (folium):

import pandas as pd
import geopandas as gpd
import folium

df_postcode_variable = pd.read_csv("postcode_sample.csv", dtype=str)  # Postcode variable level
df_postcode_variable['people'] = df_postcode_variable['people'].astype(float)
df_polygons = gpd.read_file("postcode_polygons.shp") # Polygons file
df_polygons.crs = "EPSG:4326"

my_map = folium.Map(location=[41.3890727,2.1572654], zoom_start=13,
                   detect_retina=True, control_scale=False)

folium.Choropleth(
        geo_data=df_polygons,
        name='choropleth',
        data=df_postcode_variable,
        columns=['cp','people'],
        key_on='cp',
        fill_color='OrRd',
        fill_opacity=0.5,
        line_opacity=0.8,
        line_color='Blue',
        legend_name='number of people'
    ).add_to(my_map)

my_map

But I get the following error:

ValueError: key_on `'cp'` not found in GeoJSON.

Attempt 2(pandas_bokeh) working:

import pandas_bokeh
pandas_bokeh.output_notebook()

df_joined=df_polygons.merge(df_postcode_variable.set_index('cp'), on='cp')

df_joined.plot_bokeh(simplify_shapes=20000,
                  category="people", 
                  colormap="Spectral", 
                  hovertool_columns=["cp","people"])

enter image description here

How could I fix this error on folium? cp is in both objects so, It doesnt make sense to me. It seems that is not thetecting cp in df_polygons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2025-02-16 18:12:15

如果Geodata为geopandas格式,则将密钥指定为 feature.properties.column_name ,如Geojson中。请参阅详细信息

my_map = folium.Map(location=[41.3890727,2.1572654], zoom_start=12,
                   detect_retina=True, control_scale=False)

folium.Choropleth(
        geo_data=df_polygons,
        name='choropleth',
        data=df_postcode_variable,
        columns=['cp','people'],
        key_on='feature.properties.cp',
        fill_color='OrRd',
        fill_opacity=0.5,
        line_opacity=0.8,
        line_color='Blue',
        legend_name='number of people'
    ).add_to(my_map)

my_map

If the geodata is in geopandas format, the key is specified as feature.properties.column_name, as in geojson. See this for details.

my_map = folium.Map(location=[41.3890727,2.1572654], zoom_start=12,
                   detect_retina=True, control_scale=False)

folium.Choropleth(
        geo_data=df_polygons,
        name='choropleth',
        data=df_postcode_variable,
        columns=['cp','people'],
        key_on='feature.properties.cp',
        fill_color='OrRd',
        fill_opacity=0.5,
        line_opacity=0.8,
        line_color='Blue',
        legend_name='number of people'
    ).add_to(my_map)

my_map

enter image description here

羁拥 2025-02-16 18:12:15

此错误消息暗示您的数据中的一种意外数据类型,该数据类型导致 np.isnan 抱怨引擎盖下的某个地方,可能 String

尝试将 People 列的数据类型更改为 int

df_postcode_variable['people'] = df_postcode_variable['people'].astype(int)

This error message hints at an unexpected data type in your data which is causing np.isnan to complain somewhere under the hood, probably string.

Try changing the data type of your people column to int:

df_postcode_variable['people'] = df_postcode_variable['people'].astype(int)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文