我想用matplotlib子图绘制两个GeoPandas图。这两张地图有相同的图例,因此我只想有一个图例。但是,如果我在其中一个GeoPandas图中添加图例,该图会稍微变小。这是一个问题,因为两个图的大小不同
这是我的密码:
fig,ax = plt.subplots(1, 2, figsize=(12,8))
sealand_grid.plot(column=sealand_grid['p_2012'],
ax=ax[0],
cmap='magma')
sealand_grid.plot(column=sealand_grid['p_2013'],
ax=ax[1],
cmap='magma',
legend=True,
legend_kwds={'shrink': 0.3})
ax[0].set_title('Housing prices 2012', fontsize=18)
ax[1].set_title('Housing prices 2013', fontsize=18)
fig.patch.set_facecolor('xkcd:white')
ax[0].axis('off')
ax[1].axis('off')
fig.tight_layout()
其中sealand_grid
是我的GeoPandas数据帧,p_2012
和p_2013
是两张地图中绘制的变量
如何使两张地图大小相同,而只有一个图例?
一种方法是创建用于打印颜色栏的专用轴。因此,为2个所需的绘图创建了3个轴。注意使用
gridspec_kw
和适当的width_ratios
值来获得子批次所需的宽度。下面是可运行代码和结果图使用这种方法,可以将颜色栏放置在3个轴中的任何一个轴上,但必须相应地更改代码才能完成
为了重现您的问题,我使用了这段代码,它基本上显示了相同的结果:由于颜色条的原因,右侧的图像略小于左侧的图像
它给出了这个图:
我解决了这个问题:
它给出了这个图:
基本上,我添加了第三个轴,将其关闭并添加到颜色栏。您需要注意第三个轴的位置以及方法中的参数:
fig.add_axes([0.85, 0.1, 0.1, 0.8])
。我知道这肯定不是最优雅的解决方案
编辑
一个更强大、更优雅的解决方案是保留两个轴,但在定义它们时设置它们的大小和位置:
它给出了这个图:
在这种情况下,您必须注意具有以下线的两个轴的位置和大小:
相关问题 更多 >
编程相关推荐