Geopandas一些形状没有轮廓--想要更新背景吗?

vdgimpew  于 2023-04-04  发布在  其他
关注(0)|答案(1)|浏览(111)

我正在使用Geopandas在美国Map上绘制计数。我想将绘图/图形的背景更改为黑色,但我无法在Geopandas中定位设置。
我猜我需要使用matplotlib。我筛选了这个文档,但我没有看到任何与背景相关的内容。我做错了什么?
这是我的代码:

import geopandas as gpd

us_map = gpd.read_file('tl_2020_us_state.shp')

names = ['Alaska', 'Hawaii', 'United States Virgin Islands', 'Commonwealth of the Northern Mariana Islands', 'Guam', 'American Samoa', 'Puerto Rico']

updated_us_map = us_map.loc[~(us_map)['NAME'].isin(names)]

state_occurrences = us_only_places_df['State'].value_counts().reset_index().rename(columns={'index':'NAME','State':'count'})

merge_map = updated_us_map.merge(state_occurrences, on='NAME')
merge_map.plot(column='count', cmap='Oranges', legend=True, legend_kwds={'label': "Number of Places", 'orientation': "vertical", 'shrink':0.4}, figsize=(15,20), edgecolor='black', set_facecolor = '#F0F0F0')
plt.title('Count of Places Saved for each US States')

根据建议更新代码:

import geopandas as gpd

us_map = gpd.read_file('tl_2020_us_state.shp')
names = ['Alaska', 'Hawaii', 'United States Virgin Islands', 'Commonwealth of the Northern Mariana Islands', 'Guam', 'American Samoa', 'Puerto Rico']
updated_us_map = us_map.loc[~(us_map)['NAME'].isin(names)]
state_occurrences = us_only_places_df['State'].value_counts().reset_index().rename(columns={'index':'NAME','State':'count'})
merge_map = updated_us_map.merge(state_occurrences, on='NAME')
fig, ax = plt.subplots(figsize=(2,2))
merge_map.plot(column='count', cmap='Oranges', legend=True, legend_kwds={'label': "Number of Places", 'orientation': "vertical", 'shrink':0.4}, figsize=(5,5), edgecolor='black')
plt.title('Count of Places Saved for each US States')

nlejzf6q

nlejzf6q1#

正如您提到的,您可以使用matplotlib及其模块pyplot(import matplotlib.pyplot as plt)和subplots函数。
你可以试试这样的方法:

us_map = gpd.read_file('tl_2020_us_state.shp')

names = ['Alaska', 'Hawaii', 'United States Virgin Islands', 'Commonwealth of the Northern Mariana Islands', 'Guam', 'American Samoa', 'Puerto Rico']

updated_us_map = us_map.loc[~(us_map)['NAME'].isin(names)]

merge_map = updated_us_map.merge(state_occurrences, on='NAME')

state_occurrences = us_only_places_df['State'].value_counts().reset_index().rename(columns={'index':'NAME','State':'count'})

updated_us_map = us_map.loc[~(us_map)['NAME'].isin(names)]

fig, ax = plt.subplots(figsize=(18,16))
us_map.plot(ax=ax, alpha=0.4)
merge_map.plot(column='count', cmap='Oranges', legend=True, legend_kwds={'label': "Number of Places", 'orientation': "vertical", 'shrink':0.4}, figsize=(15,20), edgecolor='black', set_facecolor = '#F0F0F0')

让我知道这是否有效,您可以使用alpha=0.4中的输入参数更改透明度

相关问题