Geopandas 图例未显示数据集中观测值为零的箱
我正在尝试对美国各州发现的鸟类数量进行分类。这些值介于 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)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Matthew 的答案之外,我发现的另一个解决方法是完全放弃 mapclassify 包并使用 pd.cut() 来对数据进行分类。无论出于何种原因,使用此方法都可以让图例显示没有观察值的箱。
然后您可以简单地调用
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.
And then you can simply call
gdf.plot('cl')
along with whatever remaining args/kwargs you want.您可以使用补丁为图例分配自定义值。
You can use patches to assign custom values to the legend.