Jupyter/Geopandas 绘图顺序破坏 figsize

3
我有一个笔记本(GitHub链接),使用geopandas绘制着不同国家的地图,并对其进行颜色编码。取决于绘图的顺序,有时它可能没有遵循我指定的figsize()大小。我在Ubuntu 20.04和Firefox上本地运行jupyter,以及在Chromium上运行的Binder和Colab中都反复看到这种行为。
请问有人能帮我理解发生了什么?这是一个错误吗?还是我控制geopandas/matplotlib的方式有误?
import matplotlib.pyplot as plt
import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
sixc = world[ world['continent'] != 'Antarctica' ]
asia = world[ world['continent'] == 'Asia' ]
noam = world[ world['continent'] == 'North America']
swed = world[ world['iso_a3'] == 'SWE' ] 

# This works, makes a 2x1 landscapey aspect
axes = sixc.plot(figsize=(8,4), color='lightgrey')
asia.plot(ax=axes, color='green')
noam.plot(ax=axes, color='purple')

# Plotting swed at the end breaks figsize, makes it squareish
axes = sixc.plot(figsize=(8,4), color='lightgrey')
asia.plot(ax=axes, color='green')
noam.plot(ax=axes, color='purple')
swed.plot(ax=axes, color='yellow')

# Plotting swed in the middle makes it ok again
axes = sixc.plot(figsize=(8,4), color='lightgrey')
asia.plot(ax=axes, color='green')
swed.plot(ax=axes, color='yellow')  
noam.plot(ax=axes, color='purple')

(此外,对于我的一些学生(但不是所有学生!),那个破碎的情节中也没有浅灰色背景的国家。这是否可能是导致该方面问题的结果/副作用?)

geopandas 的版本是多少?运行 gpd.__version__ - swatchai
gpd.version == 0.8.1。 - RubeRad
那就是我的笔记本电脑上的gpd版本,当我使用本地jupyter时,它应该是正在使用的版本(我不使用“环境”)。colab也从“!pip install”获取0.8.1。 - RubeRad
2个回答

6
使用您当前正在使用的geopandas v0.8.1版本时,您所使用的绘图命令的默认纵横比为auto。因此,您获取的输出图将具有不可预测的纵横比值。请尝试:
print(axes.get_aspect()) 

对于每个图形,以前版本的geopandas会输出equal,并且这些图形是正确的。但在您的情况下,您将获得不表示相等方面的值。

为了简单地修复您的问题,您可以添加以下代码行:

 axes.set_aspect('equal')

在最后一个绘图语句之后。

3
感谢简明的版本! - RubeRad
2
好的。谢谢! - David Erickson

1
这是一个关于如何在matplotlib中使用纵横比的优秀文章;然而,尽管这些概念很有帮助,但GeoSeries.plot是建立在matplotlib之上的,因此该答案可能无法解决问题的相关代码。
最简单的方法是将aspect='equal'传递给每个GeoSeries.plot作为默认值为链接中"aspect"的'auto'
至于您的其他问题,我无法重现,并且我建议那些学生升级到最新版本或使用不同的IDE。在Jupyter Notebook中,这对我起作用。
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
sixc = world[ world['continent'] != 'Antarctica' ]
asia = world[ world['continent'] == 'Asia' ]
noam = world[ world['continent'] == 'North America']
swed = world[ world['iso_a3'] == 'SWE' ] 

# This works, makes a 2x1 landscapey aspect
axes = sixc.plot(figsize=(8,4), color='lightgrey', aspect='equal')
asia.plot(ax=axes, color='green', aspect='equal')
noam.plot(ax=axes, color='purple', aspect='equal')

# Plotting swed at the end breaks figsize, makes it squareish
axes = sixc.plot(figsize=(8,4), color='lightgrey', aspect='equal')
asia.plot(ax=axes, color='green', aspect='equal')
noam.plot(ax=axes, color='purple', aspect='equal')
swed.plot(ax=axes, color='yellow', aspect='equal')

# Plotting swed in the middle makes it ok again
axes = sixc.plot(figsize=(8,4), color='lightgrey', aspect='equal')
asia.plot(ax=axes, color='green', aspect='equal')
swed.plot(ax=axes, color='yellow', aspect='equal')
noam.plot(ax=axes, color='purple', aspect='equal')

enter image description here

此外,需要注意的是该数据集中使用了ISO代码为99的不规则情况。我相信这个问题会在更新中得到解决(参见https://github.com/geopandas/geopandas/issues/1041)...您可以使用以下代码手动修复它们:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.loc[world['name'] == 'France', 'iso_a3'] = 'FRA'
world.loc[world['name'] == 'Norway', 'iso_a3'] = 'NOR'
world.loc[world['name'] == 'Somaliland', 'iso_a3'] = 'SOM'
world.loc[world['name'] == 'Kosovo', 'iso_a3'] = 'RKS'

1
谢谢您详细的回答和链接!那么,aspect='equal' 是不是意味着“相对于之前绘制的图形/坐标轴相等”?我本来以为它意味着“高度/宽度相等,即正方形”。(编辑)现在我明白了,它意味着 x/y 单位的等比例缩放,在这种情况下,经度展开的宽度是纬度的两倍。 - RubeRad
1
还有感谢您测试其他问题(没有浅灰色国家)。我对此感到困惑,因为我们都在Colab中运行相同的github托管笔记本,但不止一个学生得到了不同的结果! - RubeRad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接