【问题标题】:Choropleth map is not showing color variation in the outputChoropleth 地图未在输出中显示颜色变化
【发布时间】:2019-04-07 21:09:21
【问题描述】:

即使在 choropleth 与 geo_data 链接并且 data frame 中的 data 参数链接后,我的输出地图中也没有任何颜色变化>choropleth 方法。

我正确地提供了 "key_on" 参数和正确地提供了 "columns" 参数。我已经从 Dataframe 中删除了所有 NULL 值。

import pandas as pd
from pandas import read_csv
import folium
import os
import webbrowser

crimes = read_csv('Dataframe.csv',error_bad_lines=False)

vis = os.path.join('Community_Areas.geojson')
m = folium.Map(location = [41.878113, -87.629799], zoom_start = 10, tiles = "cartodbpositron")
m.choropleth(geo_data=vis, data = crimes, columns = ['Community Area', 'count'], fill_color = 'YlGn', key_on = 'feature.properties.area_numbe')
folium.LayerControl().add_to(m)
m.save('map.html')
webbrowser.open(filepath)

我希望是彩色的等值线图,但实际输出完全是灰色的。我将在下面的链接中添加代码、数据、输出。

代码链接:https://github.com/rahul0070/Stackoverflow_question_data

数据

,Community Area,count
0,25.0,92679
1,8.0,48751
2,43.0,47731
3,23.0,45943
4,29.0,44819
5,28.0,42243
6,71.0,40624
7,67.0,40157
8,24.0,39680
9,32.0,38513
10,49.0,37227
11,68.0,37023
12,69.0,35874
13,66.0,33877
14,44.0,32256
15,6.0,31043
16,26.0,30565
17,27.0,28113
18,61.0,27362
19,22.0,27329
20,46.0,26897
21,19.0,26198
22,30.0,24362
23,53.0,22645
24,42.0,21284
25,7.0,21047
26,1.0,19932
27,3.0,19799
28,15.0,17820
29,38.0,17660
30,2.0,17213
31,73.0,17071
32,16.0,15926
33,40.0,14943
34,58.0,14143
35,31.0,13934
36,63.0,13203
37,70.0,12980
38,35.0,12965
39,14.0,12714
40,77.0,12612
41,21.0,12587
42,75.0,11156
43,65.0,10812
44,51.0,10348
45,56.0,10176
46,4.0,9984
47,33.0,8987
48,60.0,8982
49,76.0,8938
50,20.0,8922
51,17.0,8536
52,41.0,8076
53,48.0,7823
54,5.0,7761
55,45.0,7485
56,39.0,7466
57,52.0,7150
58,54.0,6777
59,10.0,6372
60,11.0,6072
61,34.0,6053
62,62.0,5736
63,50.0,5730
64,59.0,5695
65,57.0,5244
66,64.0,5194
67,72.0,4962
68,37.0,4908
69,13.0,4570
70,36.0,3359
71,74.0,3145
72,55.0,3109
73,18.0,3109
74,12.0,2454
75,47.0,2144
76,9.0,1386

【问题讨论】:

  • CSV 中的区域编号具有小数点 (35.0)。在 json 文件中,数字是文本。我会在 csv 中进行查找和替换,然后去掉“.0”。在加载 csv 时,您可以添加 dtype={1:str}
  • 我尝试了那个解决方案,但是 .0 不会去。我什至尝试过使用 int() 方法。
  • 您是否使用 Excel 来编辑 csv 文件? Excel 是相当难以预测的,它可以将文本转换为数字,将数字转换为文本等。您可以在记事本中打开 csv(因为此文件非常小)并进行查找和替换。
  • 不,我使用了python的int()函数。

标签: python pandas geojson choropleth folium


【解决方案1】:

读取数据后发现您没有将社区区域列的数据类型转换为字符串类型,因为geojson文件包含字符串形式的键。所以键和列的数据类型应该匹配。

这里是您回答的完整解决方案

# importing libraries
import pandas as pd
from pandas import read_csv
import folium
import os
import webbrowser
# read the data
crimes = read_csv('Dataframe.csv',error_bad_lines=False)
# convert float to int then to string
crimes['Community Area'] = crimes['Community Area'].astype('int').astype('str')
# choropleth map
vis = 'Community_Areas.geojson'
m = folium.Map(location = [41.878113, -87.629799], zoom_start = 10, tiles = "cartodbpositron")
m.choropleth(geo_data=vis, data = crimes, columns = ['Community Area', 'count'], fill_color = 'YlGn', key_on = 'feature.properties.area_num_1')
folium.LayerControl().add_to(m)
m.save('map.html')
webbrowser.open('map.html')

如果您发现难以理解代码的任何部分,请发表评论。对于key_on参数你也可以试试feature.properties.area_numbe

【讨论】:

  • 非常感谢您的解决方案有效!但我有一个疑问。我实际上尝试按照您所说的转换列的类型,但它不会转换。我尝试在读取 CSV 文件时使用 dtype,还尝试使用 for 循环迭代每个变量并使用 str() 方法转换每个元素。如果您对我的方法为什么不起作用有任何想法,请告诉我。 @Himanshu
  • 尝试删除 str 和 int 的引号。当您尝试这样做时遇到了什么错误?还要提及您正在使用的 pandas 版本。
  • 熊猫版本:0.24.2。我没有收到任何错误,数据类型根本不会改变。我什至使用了以下代码: df['column'] = df['column'].astype('str') ,但得到了相同的结果
  • 尝试删除引号,或者您可以使用 pandas df['column'].apply 函数
猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 2020-09-01
  • 2022-06-29
  • 1970-01-01
  • 2020-07-09
  • 2015-01-02
相关资源
最近更新 更多