【问题标题】:Shared legend in GeoPandas multiplotGeoPandas 多图中的共享图例
【发布时间】:2020-03-04 17:49:48
【问题描述】:

我正在为每个月创建一个带有 GeoPandas GeoDataFrame.plot() 子图的多图。如何为所有子图共用图例以及 x 和 y 轴并控制 figsize?。

我知道有sharex=Truesharey=True,但我不知道放在哪里。

plt.figure(sharex=True, sharey=True) 返回TypeError: __init__() got an unexpected keyword argument 'sharex'

world.plot(column='pop_est', ax=ax, legend=True, sharex=True, sharey=True) 返回AttributeError: 'PatchCollection' object has no property 'sharex'

ax = plt.subplot(4, 3, index + 1, sharex=True, sharey=True) 返回TypeError: cannot create weak reference to 'bool' object

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

months = pd.DataFrame(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
                      columns = ['months'])

plt.figure()

for index, i in months.iterrows():
    ax = plt.subplot(4, 3, index + 1) # nrows, ncols, axes position
    world.plot(column='pop_est', ax=ax, legend=True)
    ax.set_title(index)
    ax.set_aspect('equal', adjustable='datalim')

plt.suptitle('This is the figure title', fontsize=12, y=1.05)
plt.tight_layout()
plt.show()

【问题讨论】:

  • sharexsharey 可以用作plt.subplotsfig, ax_array = plt.subplots(4,3, sharex=True, sharey=True) 的参数。但是,这不会创建共享图例。
  • 它也对我不起作用。 ax = plt.subplot(4, 3, index + 1, sharex=True, sharey=True) yiels TypeError: cannot create weak reference to 'bool' object.
  • 是的,因为我说plt.subplots
  • 感谢您的支持。但是,将ax = plt.subplot(4, 3, index + 1) 替换为fig, ax_array = plt.subplots(4,3, sharex=True, sharey=True)fig, ax_array = plt.subplots(4,3, index + 1, sharex=True, sharey=True) 会分别导致空白画布和TypeError: subplots() got multiple values for argument 'sharex'
  • 这需要在循环之外,随后您需要在 ax_array 内的轴上循环。但如前所述,这只是为了显示您的错误来自哪里,它不会创建任何共享图例或类似内容。

标签: python matplotlib geopandas


【解决方案1】:

shared_xshare_y 关键字已经在上面的 cmets 中由 @ImportanceOfBeingErnest 进行了解释。效果很好,如果您实际上是在展示整个世界,我会亲自删除所有刻度/标签。但对于子集,它可能会有所帮助。

创建一个共享图例(Matplotlib 将其称为颜色条,而不是图例)有点挑剔,因为 Geopandas 不返回轴而不是 mapabble(就像 Matplotlib 通常那样)。一个简单的解决方法是从轴集合中获取它,但这确实需要一些假设,如果有多个,您需要哪个可映射。

在这种情况下,每个轴只有 1 个(补丁集合),并且每个轴都相同。因此,使用axs[0].collections[0] 获取 ith 很简单,但不是最强大的解决方案。

另一种方法是使用cax=cax 关键字预先创建cax 并将其提供给Geopandas 图。但是手动创建cax(使用 fig.add_axes([]) 不太方便。

如下所示使用fig.colorbar 似乎更容易,因为它允许基于轴列表/数组创建cax

fig, axs = plt.subplots(4,3, figsize=(10,7), 
                        facecolor='w',
                        constrained_layout=True, 
                        sharex=True, sharey=True, 
                        subplot_kw=dict(aspect='equal'))

axs = axs.ravel()

fig.suptitle('This is the figure title', fontsize=12, y=1.05)

for index, i in months.iterrows():

    axs[index].set_title(index)
    world.plot(column='pop_est', ax=axs[index])


# assume it's the first (and only) mappable
patch_col = axs[0].collections[0]
cb = fig.colorbar(patch_col, ax=axs, shrink=0.5)

【讨论】:

  • 谢谢你,太棒了 :) 我无法运行 ImportanceOfBeingErnest 的代码片段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2016-09-17
  • 2020-06-30
  • 1970-01-01
相关资源
最近更新 更多