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

Folium Choropleth Map - Is there a way to add crosshatching for NaN values?

有没有办法将交叉影线(交叉线)指定为 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

Link到美国-states.json数据:

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

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

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