首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义Geopandas中的图例标签

自定义Geopandas中的图例标签
EN

Stack Overflow用户
提问于 2022-10-20 17:05:51
回答 2查看 129关注 0票数 0

我想定制的标签上的地质公园的情节传说。

代码语言:javascript
运行
复制
fig, ax = plt.subplots(figsize = (8,5))

gdf.plot(column = "WF_CEREAL", ax = ax, legend=True, categorical=True, cmap='YlOrBr',legend_kwds = {"loc":"lower right"}, figsize =(10,6))

"labels"中添加legend_kwds没有帮助。

我试着用以下方式添加带有legend_kwds的标签,但是没有用-

legend_kwds = {"loc":"lower right", "labels":["low", "mid", "high", "strong", "severe"]

legend_labels:["low", "mid", "high", "strong", "severe"]

legend_labels=["low", "mid", "high", "strong", "severe"]

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-21 12:52:36

因为这个问题没有可重复的代码和数据来处理。我将使用最好的方法,给出一个演示代码,一般读者可以遵循,其中一些可能会回答问题。

下面我提供的代码可以运行,而不需要外部数据。注释被插入不同的地方,以便在重要的步骤中进行解释。

代码语言:javascript
运行
复制
# Part 1
# Classifying the data of choice

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est

num_classes = 4   #quartile scheme has 4 classes

# You can use values derived from your preferred classification scheme here
num_qtiles = [0, .25, .5, .75, 1.]   #class boundaries for quartiles
# Here is the categorical data to append to the dataframe
# They are also used as legend's label texts
qlabels = ["1st quartile","2nd quartile","3rd quartile","4th quartile"]  #matching categorical data/labels

# Conditions
# len(num_qtiles)-1 == num_classes
# len(qlabels) == num_classes

# Create a new column for the categorical data mentioned above
world['gdp_quartile'] = pd.qcut(world['gdp_per_cap'], num_qtiles, labels=qlabels)

# Plotting the categorical data for checking
ax1 = world['gdp_quartile'].value_counts().plot(figsize=(5,4), kind='bar', xlabel='Quartile_Classes', ylabel='Countries', rot=45, legend=True)

第1部分的输出:-

代码语言:javascript
运行
复制
# Part 2
# Plot world map using the categorical data

fig, ax = plt.subplots(figsize=(9,4))

# num_classes = 4 # already defined
#color_steps = plt.colormaps['Reds']._resample(num_classes)   #For older version
color_steps = plt.colormaps['Reds'].resampled(num_classes)    #Current version of matplotlib

# This plots choropleth map using categorical data as the theme
world.plot(column='gdp_quartile', cmap = color_steps, 
           legend=True, 
           legend_kwds={'loc':'lower left', 
                        'bbox_to_anchor':(0, .2), 
                        'markerscale':1.29, 
                        'title_fontsize':'medium', 
                        'fontsize':'small'}, 
           ax=ax)

leg1 = ax.get_legend()
leg1.set_title("GDP per capita")
ax.title.set_text("World Map: GDP per Capita")
plt.show()

第2部的输出:-

编辑

其他代码,使用它替换上面的行plt.show()。这回答了以下评论中的问题。

代码语言:javascript
运行
复制
# Part 3
# New categorical texts to use with legend
new_legtxt = ["low","mid","high","v.high"]
for ix,eb in enumerate(leg1.get_texts()):
    print(eb.get_text(), "-->", new_legtxt[ix])
    eb.set_text(new_legtxt[ix])

plt.show()
票数 1
EN

Stack Overflow用户

发布于 2022-10-20 22:20:35

我不确定这是否可行,但请尝试:

代码语言:javascript
运行
复制
gdf.plot(column = "WF_CEREAL", ax = ax, legend=True, categorical=True, cmap='YlOrBr',legend_kwds = {"loc":"lower right"}, figsize =(10,6), legend_labels=["low", "mid", "high", "strong", "severe"])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74143732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档