python - Folium Choropleth Map - 有没有办法为 NaN 值添加交叉影线?

标签 python data-visualization folium choropleth

有没有办法将交叉影线(交叉线)指定为nan_fill_color?我正在创建一些灰度可视化,目前使用白色或黑色作为填充颜色并不能完全传达我想要的含义。

这是我的代码:

state_geo = 'us-states.json'
state_unemployment = 'myData.csv'
state_data = pd.read_csv(state_unemployment)

m = folium.Map(location=[39, -98], zoom_start=4)

folium.Choropleth(
    geo_data=state_geo,
    name='choropleth',
    data=state_data,
    columns=['State', 'unemployment'],
    key_on='feature.id',
    fill_color='Greys',
    fill_opacity=1,
    line_opacity=.1,
    line_weight = 2,
    nan_fill_color='Black',
    legend_name='Year'
).add_to(m)

folium.LayerControl().add_to(m)

m

链接到 us-states.json 数据:

https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/us-states.json

最佳答案

注意:此解决方案使用 geopandas。可能是更好的方法,希望有人会发布它。

import pandas as pd
import geopandas as gpd
import numpy as np
import folium
from folium.plugins import StripePattern

# mock user data
state_data = pd.DataFrame([['NE', .5],
                            ['IA', .23],
                            ['MO', np.nan],
                            ['KS', np.nan]],
                          columns=['State', 'unemployment'])


state_geo = 'us-states.json'

m = folium.Map(location=[39, -98], zoom_start=4)

folium.Choropleth(
    geo_data=state_geo,
    name='choropleth',
    data=state_data,
    columns=['State', 'unemployment'],
    key_on='feature.id',
    fill_color='Greys',
    fill_opacity=1,
    line_opacity=.1,
    line_weight = 2,
    nan_fill_color='White',
    legend_name='Year'
).add_to(m)

# geojson to geopandas dataframe
gdf = gpd.read_file(state_geo)

# creating a list of NaNs in state_data which we will user to filter our geodataframe
nans = state_data[state_data['unemployment'].isnull()]['State'].values
gdf_nans = gdf[gdf['id'].isin(nans)]

# Not exactly a crosshatch, but its close.  Maybe you can find a better pattern
sp = StripePattern(angle=45, color='grey', space_color='white')
sp.add_to(m)

# adding the nan layer with `sp` as the fillPattern
folium.GeoJson(data=gdf_nans, style_function=lambda x :{'fillPattern': sp}).add_to(m)

folium.LayerControl().add_to(m)
m

enter image description here

关于python - Folium Choropleth Map - 有没有办法为 NaN 值添加交叉影线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57314597/

相关文章:

python - 如何在 django 中创建一个将转到引用 View 的 URL 链接?

python - Python中if语句的求值顺序

r - 在 R 中可视化纵向分类数据的好方法

python - 为什么在 Folium 中使用超过 100 个圆形标记进行绘图会导致 map 空白?

python - 在 Folium 中突出显示一个特定国家

python - 如何将记录数组从 arff 文件转换为 ndarray?

python - 使用jupyter在python3中用字符串替换标准输入

python - 使用 hexbin 的 Pairplot

c# - 在饼图中隐藏标签(MS Chart for .Net)

python - 无法在 folium map 中将 altair 可视化作为弹出窗口插入