【发布时间】:2020-07-06 06:09:13
【问题描述】:
我找到了许多关于如何避免在 matplotlib 上重叠文本的示例,但没有一个我可以弄清楚如何应用于我的案例。
我有一个数据框 (mapadf1),其中包含有关巴西市政当局的一些信息,并且我绘制了一个圣保罗州 (sp) 的 shapefile。
我创建了一个变量“l”,其中包含自治市的名称和我要突出显示的数字。当数字为0时,字符串为空。
好的,所以我设法用以下代码绘制了我的地图:
# set the range for the choropleth values
vmin, vmax = 0, 1
# create figure and axes for Matplotlib
fig, ax = plt.subplots(1, figsize=(30, 10))
# remove the axis que mostra latitude e longitude
ax.axis('off')
# add a title and annotation
ax.set_title('Número leitos inaugurados: 22/03', fontdict={'fontsize': '25', 'fontweight' : '3'})
ax.annotate('Fonte: Governo do Estado de São Paulo', xy=(0.6, .05), xycoords='figure fraction', fontsize=12, color='#555555')
# empty array for the data range
sm.set_array([]) # or alternatively sm._A = []. Not sure why this step is necessary, but many recommends it
# create map
mapa_df1.plot(column='tem_leito',cmap='Paired', linewidth=0.8, ax=ax, edgecolor='0.8')
# Add Labels
mapa_df1['coords'] = mapa_df1['geometry'].apply(lambda x: x.representative_point().coords[:])
mapa_df1['coords'] = [coords[0] for coords in mapa_df1['coords']]
for idx, row in mapa_df1.iterrows():
plt.annotate(s=row['l'], xy=row['coords'])
还有我的地图:
如何避免文字重叠?!
提前致谢!
【问题讨论】:
标签: python matplotlib matplotlib-basemap geopandas