[英]Adding legend in geopandas plot with subplots changes size of plot
I want to plot two GeoPandas plots with matplotlib subplots.我想 plot 两个带有 matplotlib 子图的 GeoPandas 图。 The two maps have the same legend, and therefore I only want to have one legend.
两张地图有相同的图例,因此我只想有一个图例。 However, if I add a legend to one of the GeoPandas plots, the plot becomes slightly smaller.
但是,如果我在 GeoPandas 图中添加一个图例,则 plot 会稍微变小。 This is a problem since the two plots then become different sizes.
这是一个问题,因为这两个地块的大小不同。
Here is my code:这是我的代码:
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()
where sealand_grid
is my GeoPandas-dataframe, and p_2012
and p_2013
are the variables plotted in the two maps.其中
sealand_grid
是我的 GeoPandas 数据框, p_2012
和p_2013
是绘制在两张地图中的变量。
How do I get the two maps to be the same size, while only having one legend?如何使两张地图的大小相同,而只有一个图例?
In order to reproduce you issue, I used this code, which basically shows the same result: the image on the right is slightly smaller than the left one due to the colorbar.为了重现您的问题,我使用了这段代码,它基本上显示了相同的结果:由于颜色条,右侧的图像比左侧的图像略小。
import matplotlib.pyplot as plt
import numpy as np
D2012 = np.random.rand(10, 10)
D2013 = np.random.rand(10, 10)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16,8))
P2012 = ax1.imshow(D2012,
cmap = 'magma')
P2013 = ax2.imshow(D2013,
cmap = 'magma')
ax1.set_title('2012', fontsize = 18)
ax2.set_title('2013', fontsize = 18)
ax1.axis('off')
ax2.axis('off')
plt.colorbar(P2013)
plt.show()
which gives this plot:这给出了这个 plot:
I solved with this turnaround:我解决了这个转变:
import matplotlib.pyplot as plt
import numpy as np
D2012 = np.random.rand(10, 10)
D2013 = np.random.rand(10, 10)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16,8))
ax3 = fig.add_axes([0.85, 0.1, 0.1, 0.8])
P2012 = ax1.imshow(D2012,
cmap = 'magma')
P2013 = ax2.imshow(D2013,
cmap = 'magma')
ax1.set_title('2012', fontsize = 18)
ax2.set_title('2013', fontsize = 18)
ax1.axis('off')
ax2.axis('off')
ax3.axis('off')
plt.colorbar(P2013, ax = ax3)
plt.show()
which gives this plot:这给出了这个 plot:
Basically, I add a third axis, turn it off and add to it the colorbar.基本上,我添加了第三个轴,将其关闭并添加颜色条。 You need to pay attention to the position of this third axis with the parameters inside the method:
fig.add_axes([0.85, 0.1, 0.1, 0.8])
.你需要注意这个第三轴的 position 方法里面的参数:
fig.add_axes([0.85, 0.1, 0.1, 0.8])
。
I know this is not the most elegant solution, for sure.我知道这肯定不是最优雅的解决方案。
EDIT编辑
A more robust and elegant solution is to keep 2 axes, but set their size and position when you define them:一个更健壮和优雅的解决方案是保留 2 个轴,但在定义它们时设置它们的大小和 position:
import matplotlib.pyplot as plt
import numpy as np
D2012 = np.random.rand(10, 10)
D2013 = np.random.rand(10, 10)
fig = plt.figure(figsize = (16,8))
ax1 = fig.add_axes([0, 0.2, 0.6, 0.6])
ax2 = fig.add_axes([0.4, 0.2, 0.6, 0.6])
P2012 = ax1.imshow(D2012,
cmap = 'magma')
P2013 = ax2.imshow(D2013,
cmap = 'magma')
ax1.set_title('2012', fontsize = 18)
ax2.set_title('2013', fontsize = 18)
ax1.axis('off')
ax2.axis('off')
plt.colorbar(P2013)
plt.show()
which gives this plot:这给出了这个 plot:
In this case you have to pay attention to the position and the size of the two axis with these lines:在这种情况下,您必须注意 position 和这些线的两个轴的大小:
ax1 = fig.add_axes([0, 0.2, 0.6, 0.6])
ax2 = fig.add_axes([0.4, 0.2, 0.6, 0.6])
One approach is to create an exclusive axes for plotting the color bar.一种方法是创建一个专用轴来绘制颜色条。 So 3 axes are created for 2 required plots.
因此为 2 个所需的图创建了 3 个轴。 Here is the runnable code and the resulting plot.
这是可运行代码和生成的 plot。
import numpy as np
import matplotlib.pyplot as plt
# create simple data for demo plot
data = np.arange(100, 0, -1).reshape(10, 10)
# figsize plays important role; must set it wide enough
# here 3 axes are created
# the width ratio of the 3rd axes must be small to get good result (here = .03)
fig, ax = plt.subplots(1, 3, figsize=(9.5, 4), gridspec_kw={"width_ratios":[0.4535, 0.4535, 0.03]})
# first 2 axes are used to plot images
im0 = ax[0].imshow(data, cmap='bone')
im1 = ax[1].imshow(data, cmap='bone')
# The third axes, ax[2] is exclusive to the color bar
# When cax id specified, 'shrink' and 'aspect' properties are ignored
cb = fig.colorbar(im0, cax=ax[2], orientation='vertical')
plt.show()
With this approach, the color bar can be placed on any of the 3 axes, but you have to change the code accordingly to get it done.使用这种方法,颜色条可以放置在 3 个轴中的任何一个上,但您必须相应地更改代码才能完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.