在我正在创建的 geopandas 世界 choropleth 中,我想用颜色条编辑多项内容。即大小(使其与地图本身大致匹配)、文本大小(当前显示太小),我还想为其添加标签。从阅读中似乎没有简单的方法可以在 geopandas 中明确地做到这一点,所以我想知道是否有人可以提供任何解决方法?任何帮助将不胜感激。
这是我目前正在使用的一段代码(在某些情况下,中间部分只是将列表与按国家/地区划分的数字合并到框架中,我将其用作着色的基础)
fig, ax = plt.subplots(figsize=(40,29.171))
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world=pd.DataFrame(world)
world['LEADS']=0.1
world = world.set_index('name')
world['name']=world.index
for x, y in zip(Countries, country_counts):
world.loc[x, 'LEADS'] = y
world=geopandas.GeoDataFrame(world)
world = world[(world.index != "Antarctica")]
ax.set_axis_off()
#ax.legend(title="Leads")
world.plot(ax=ax, column='LEADS', cmap='Oranges', legend=True,linewidth = 1.5)
plt.tight_layout()
plt.savefig('plot_image.png', bbox_inches='tight', transparent=True)
Run Code Online (Sandbox Code Playgroud)
这里的理想解决方案是保持图例选项具有的颜色,指定大小和文本大小并在栏顶部启用标签。不幸的是,我正在努力弄清楚这些是如何完成的。
我在数据集中有一列关于分类公司规模的列,目前看起来像这样,其中“-”连字符当前表示缺失的数据:
我想用空值更改缺失值中的“-”,以便我可以分析缺失的数据。但是,当我使用带有 None 值的 pd 替换工具(请参阅以下代码)时,它似乎也会生成任何真正的条目,因为它们还包含连字符(例如 51-200)。
df['Company Size'].replace({'-': None},inplace =True, regex= True)
Run Code Online (Sandbox Code Playgroud)
如何仅替换单独的连字符并保持其他条目不变?