Python 蟒蛇-叶状脉络图-颜色不正确
我的问题是郊区没有在地图上显示正确的颜色。例如,Dandenong和Frankston应使用最深的颜色进行着色,因为它们在数据帧中的计数最高,但它们使用较浅的颜色进行着色 数据帧缺少一些数据。那些郊区被涂上了最深的颜色 另一个问题是csv的所有郊区都是大写的,但geojson混合了“Frankston”、“St Kilda”或“McKinnon”。如果choropleth代码不关心这个案例,这将是很有帮助的。我可以将数据框中的文本改为“FRANKSTON”、“FRANKSTON”和“ST KILDA”、“ST KILDA”,但事实证明,“MCKINNON”改为“MCKINNON”有点棘手 创建数据帧Python 蟒蛇-叶状脉络图-颜色不正确,python,dataframe,geojson,choropleth,folium,Python,Dataframe,Geojson,Choropleth,Folium,我的问题是郊区没有在地图上显示正确的颜色。例如,Dandenong和Frankston应使用最深的颜色进行着色,因为它们在数据帧中的计数最高,但它们使用较浅的颜色进行着色 数据帧缺少一些数据。那些郊区被涂上了最深的颜色 另一个问题是csv的所有郊区都是大写的,但geojson混合了“Frankston”、“St Kilda”或“McKinnon”。如果choropleth代码不关心这个案例,这将是很有帮助的。我可以将数据框中的文本改为“FRANKSTON”、“FRANKSTON”和“ST KIL
import csv
import pandas as pd
csv_path='Data_tables_Criminal_Incidents_Visualisation_year_ending_June_2018.csv'
df=pd.read_csv(csv_path)
with open(csv_path, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# create a list of headings from the first row of the csv file
headings = next(csvreader)
# create a dictionary, where keys are Suburb/Town Name and values are number of occurences
# index 2 of the headings list are the suburbs
neighborhood_dict = df[headings[2]].value_counts().to_dict()
# make first letter uppercase eg St Kilda
neighborhood_dict = dict((k.title(), v) for k, v in neighborhood_dict.items())
# make neighborhood_list from neighborhood_dict
neighborhood_list=[]
for key, value in neighborhood_dict.items():
temp = [key,value]
neighborhood_list.append(temp)
# make dataframe from neighborhood_list
df = pd.DataFrame(neighborhood_list, columns=['Suburb','Count'])
print(df.to_string())
创建地图
import folium
world_map = folium.Map(
location=[-38.292102, 144.727880],
zoom_start=6,
tiles='openstreetmap'
)
world_map.choropleth(
geo_data='vic.geojson',
data=df,
columns=['Suburb','Count'],
key_on='feature.properties.Suburb_Name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Crime Rate in Victoria'
)
world_map.save('index.html')
我把一切都弄明白了。缺少的值是灰色的,图例是根据我选择的时间间隔定制的。清理geojson、删除尾随空白以及使所有郊区名称都大写解决了许多问题 创建字典
import pandas as pd
import csv
csv_path='Data_tables_Criminal_Incidents_Visualisation_year_ending_June_2018.csv'
df=pd.read_csv(csv_path)
# sum the number of incidents recorded for each suburb
df=df.groupby(['Suburb/Town Name'])['Incidents Recorded'].agg(
# make the numbers numeric otherwise it just concatenates strings
lambda x: pd.to_numeric(x, errors='coerce').sum()
)
# create a dictionary, where keys are Suburb/Town Name and values are number of incidents
suburb_dict = df.to_dict()
风格功能
def style_function(feature):
suburb = suburb_dict.get(feature['properties']['Suburb_Name'])
return {
'fillColor': '#gray' if suburb is None else colormap(suburb),
'fillOpacity': 0.6,
#borders
'weight': 0.2,
}
叶状图
import folium
world_map = folium.Map(
location=[-38.292102, 144.727880],
zoom_start=6,
tiles='openstreetmap'
)
folium.GeoJson(
data = 'vic_for_crime_2018.geojson',
style_function = style_function
).add_to(world_map)
彩色地图
import branca
colormap = branca.colormap.linear.YlOrRd_09.scale(0, 8500)
colormap = colormap.to_step(index=[0, 1000, 3000, 5000, 8500])
colormap.caption = 'Incidents of Crime in Victoria (year ending June 2018)'
colormap.add_to(world_map)
world_map.save('vic_final.html')
作为Frankston的付款人,感谢您将Frankston推向市场:)