【问题标题】:Jupyter/Geopandas plot order breaks figsizeJupyter/Geopandas 的绘图顺序中断了 figsize
【发布时间】:2021-01-08 21:35:01
【问题描述】:

我有一个笔记本 (github link),我在其中使用 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__
  • gpd.__version__ == 0.8.1
  • 也就是说,这就是我笔记本电脑上的gpd版本,大概是我使用本地jupyter时正在使用的(我不做'环境')。 colab 也从 "!pip install" 获得 0.8.1

标签: python matplotlib geopandas figsize


【解决方案1】:

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

print(axes.get_aspect()) 

对于每个情节。在以前版本的 geopandas 中,将得到equal 作为输出,并且绘图是正确的。但在您的情况下,您将获得不代表相等方面的值。

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

 axes.set_aspect('equal')

在最后一个情节语句之后。

【讨论】:

  • 感谢简洁版!
  • 好一个。谢谢!
【解决方案2】:

这是一个很棒的answermatplotlib 中的纵横比工作原理;然而,尽管这些概念很有帮助,但 GeoSeries.plot 是建立在 matplotlib 之上的,因此就解决问题的代码而言,这个答案可能无济于事。

最简单的做法是将aspect='equal' 作为default is 'auto' for "aspect" in link) 传递给每个GeoSeries.plot

至于您的其他问题,我无法重现,我会让那些学生升级到最新版本或使用不同的 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')

另外,作为旁注,数据集与99 作为ISO 代码存在一些违规行为。我相信它会在更新中得到修复(参见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'

【讨论】:

  • 感谢详细答案和链接!那么 aspect='equal' 是否意味着“等于先前图中的图形/轴中已经存在的内容”?我会假设它的意思是“相等的高度/宽度,即正方形”(编辑)我现在看到,这意味着 x/y 单位的比例相等,在这种情况下,经度的宽度是纬度的两倍
  • 也感谢测试其他问题(没有浅灰色国家)。我对此感到困惑,因为我们都在 Colab 中运行同一个 github 托管的笔记本,而且不是一个学生而是两个学生得到了不同的结果!
猜你喜欢
  • 2017-01-06
  • 2021-08-31
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
  • 2016-09-18
  • 2016-11-10
  • 1970-01-01
  • 2021-05-26
相关资源
最近更新 更多