Jupyter/Geopandas 绘图顺序打破了 Figsize

Rub*_*Rad 3 python matplotlib geopandas figsize

我有一个笔记本(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')
Run Code Online (Sandbox Code Playgroud)

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

swa*_*hai 6

对于geopandas您使用的 v 0.8.1,default您使用的绘图命令的纵横比是auto。因此,您获得的输出图将具有不可预测的纵横比值。尝试

print(axes.get_aspect()) 
Run Code Online (Sandbox Code Playgroud)

对于每个情节。在 geopandas 的早期版本中,将得到equal输出,并且绘图是正确的。但就您而言,您将得到不代表平等方面的值。

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

 axes.set_aspect('equal')
Run Code Online (Sandbox Code Playgroud)

在最后一个情节陈述之后。

  • 谢谢简洁版本! (3认同)
  • 好一个。谢谢! (2认同)