[英]Adding legend in geopandas plot with subplots changes size of plot
我想 plot 兩個帶有 matplotlib 子圖的 GeoPandas 圖。 兩張地圖有相同的圖例,因此我只想有一個圖例。 但是,如果我在 GeoPandas 圖中添加一個圖例,則 plot 會稍微變小。 這是一個問題,因為這兩個地塊的大小不同。
這是我的代碼:
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
是繪制在兩張地圖中的變量。
如何使兩張地圖的大小相同,而只有一個圖例?
為了重現您的問題,我使用了這段代碼,它基本上顯示了相同的結果:由於顏色條,右側的圖像比左側的圖像略小。
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()
這給出了這個 plot:
我解決了這個轉變:
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()
這給出了這個 plot:
基本上,我添加了第三個軸,將其關閉並添加顏色條。 你需要注意這個第三軸的 position 方法里面的參數: fig.add_axes([0.85, 0.1, 0.1, 0.8])
。
我知道這肯定不是最優雅的解決方案。
編輯
一個更健壯和優雅的解決方案是保留 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()
這給出了這個 plot:
在這種情況下,您必須注意 position 和這些線的兩個軸的大小:
ax1 = fig.add_axes([0, 0.2, 0.6, 0.6])
ax2 = fig.add_axes([0.4, 0.2, 0.6, 0.6])
一種方法是創建一個專用軸來繪制顏色條。 因此為 2 個所需的圖創建了 3 個軸。 這是可運行代碼和生成的 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()
使用這種方法,顏色條可以放置在 3 個軸中的任何一個上,但您必須相應地更改代碼才能完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.