Geopandas 图例未显示数据集中观测值为零的箱

发布于 2025-01-09 00:27:37 字数 1031 浏览 3 评论 0原文

我正在尝试对美国各州发现的鸟类数量进行分类。这些值介于 250 和 750 之间,因此我将它们分为 10 个容器,每个容器有 50 个(使用 MapClassify 的 UserDefined 分类器)。这是生成绘图的代码,除了图例之外,该代码运行良好:

ud_10 = mc.UserDefined(gdf['NumSpecies'], bins=np.arange(300, 800, 50), lowest=250)
gdf['cl'] = ud_10.yb # This creates a column that displays the bin number for each observation

vmin, vmax = gdf['cl'].agg(['min', 'max'])
gdf.drop['AK', 'HI'].plot('cl', ax=continental_ax, legend=True, categorical=True, 
                          cmap='viridis_r', legend_kwds:{'loc': 'lower right'})
gdf.loc[['AK']].plot(column='cl', ax=alaska_ax, cmap=colormap, vmax=vmax, vmin=vmin)
gdf.loc[['HI']].plot(column='cl', ax=hawaii_ax, cmap=colormap, vmax=vmax, vmin=vmin)

以及绘图: 输入图片这里的描述

那么,看看图例是如何缺少数字 0 和 7 的吗?这些是上面“大陆”绘图调用中的数据中缺少的数据(单独绘制的夏威夷位于 bin 0 中,并且 bin 7 中根本没有数据)。那么,geopandas 图例似乎没有考虑任何观测值 = 0 的 bin。你知道我可以用什么方法来解决这个问题吗?

非常感谢您提供的任何帮助!

I'm trying to classify the number of bird species found in US states. The values fall between 250 and 750, so I'm dividing them up into 10 bins of 50 (using MapClassify's UserDefined classifier). Here's the code that generates the plot, which, besides the legend, is coming through fine:

ud_10 = mc.UserDefined(gdf['NumSpecies'], bins=np.arange(300, 800, 50), lowest=250)
gdf['cl'] = ud_10.yb # This creates a column that displays the bin number for each observation

vmin, vmax = gdf['cl'].agg(['min', 'max'])
gdf.drop['AK', 'HI'].plot('cl', ax=continental_ax, legend=True, categorical=True, 
                          cmap='viridis_r', legend_kwds:{'loc': 'lower right'})
gdf.loc[['AK']].plot(column='cl', ax=alaska_ax, cmap=colormap, vmax=vmax, vmin=vmin)
gdf.loc[['HI']].plot(column='cl', ax=hawaii_ax, cmap=colormap, vmax=vmax, vmin=vmin)

And the plot:
enter image description here

So, see how the legend is missing the numbers 0 and 7? Those are the ones absent from the data in the "continental" plot call above (Hawaii, plotted separately, is in bin 0, and there's no data at ALL that fall in bin 7). It seems, then, that the geopandas legend does not take into account any bins for which observations = 0. Do you know of any way I can remedy this?

Thank you so, so much for any help you can provide!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里兽 2025-01-16 00:27:37

除了 Matthew 的答案之外,我发现的另一个解决方法是完全放弃 mapclassify 包并使用 pd.cut() 来对数据进行分类。无论出于何种原因,使用此方法都可以让图例显示没有观察值的箱。

bins = np.arange(250, 800, 50)
labels = np.arange(0, 10)

gdf['cl'] = pd.cut(gdf['NumSpecies'], bins=bins, labels=labels)

然后您可以简单地调用 gdf.plot('cl') 以及您想要的任何剩余参数/kwargs。

In addition to Matthew's answer, another workaround I've found is to ditch the mapclassify package altogether and use pd.cut() to categorize your data instead. For whatever reason, using this method allowed the legend to display bins that had no observations.

bins = np.arange(250, 800, 50)
labels = np.arange(0, 10)

gdf['cl'] = pd.cut(gdf['NumSpecies'], bins=bins, labels=labels)

And then you can simply call gdf.plot('cl') along with whatever remaining args/kwargs you want.

‘画卷フ 2025-01-16 00:27:37

您可以使用补丁为图例分配自定义值。

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import geopandas as gpd
from shapely.geometry import LineString

coords = [LineString([(0,1), (1,2), (2,3), (3,4)]),
          LineString([(4,5), (5,6), (6,7), (7,8)]),
          LineString([(7,1), (6,2), (5,4), (4,3)])]

gdf = gpd.GeoDataFrame(geometry=coords)
gdf.plot(color=['red', 'orange', 'blue'])

# keys are color you want, values are legend labels
patch_dict = {'red':'0', 'orange':'1', 'blue':'2'}

patch_list = []
for k, v in patch_dict.items():

    patch_list.append(mpatches.Patch(color=k, label=v))
plt.legend(handles=patch_list, loc='upper left')

输入图片此处描述

You can use patches to assign custom values to the legend.

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import geopandas as gpd
from shapely.geometry import LineString

coords = [LineString([(0,1), (1,2), (2,3), (3,4)]),
          LineString([(4,5), (5,6), (6,7), (7,8)]),
          LineString([(7,1), (6,2), (5,4), (4,3)])]

gdf = gpd.GeoDataFrame(geometry=coords)
gdf.plot(color=['red', 'orange', 'blue'])

# keys are color you want, values are legend labels
patch_dict = {'red':'0', 'orange':'1', 'blue':'2'}

patch_list = []
for k, v in patch_dict.items():

    patch_list.append(mpatches.Patch(color=k, label=v))
plt.legend(handles=patch_list, loc='upper left')

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文