matplotlib geopandas子图大小不相等

时间:2020-10-30 18:29:19

标签: python pandas matplotlib geopandas

我正在绘制四个子图(如下图),这些图显示了一些地图。当我为每个子图添加一个colobar时,最终子图(右下)的大小与其他子图不同。是什么控制了这一点,matplotlib有没有办法自动将它们缩放到相同的大小?

shp = "Coutries.shp"

map_df = gpd.read_file(shp)

Data = "CountryData.csv"
columns = ['Country', 'Percent_1', 'Percent_2', 'Percent_3', 'Percent_4']
df = pd.read_csv(Data,usecols=columns)

# Merge data and remove NANs
merged = map_df.set_index('NAME_LONG').join(df.set_index('Country'))
merged = merged.fillna(-9999)

Variable1 = merged[merged.GMWPercent!=-9999]
Variable2 = merged[merged.GiriPercent!=-9999]
Variable3 = merged[merged.HamiltonPercent!=-9999]
Variable4 = merged[merged.SpaldingPercent!=-9999]


f, axes = plt.subplots(figsize=(10, 5), ncols=2, nrows=2)

Variable1.plot(column='Percent_1', ax=axes[0,0])
Variable2.plot(column='Percent_2', ax=axes[1,0])
Variable3.plot(column='Percent_3', ax=axes[0,1])
Variable4.plot(column='Percent_4', ax=axes[1,1])


sm = plt.cm.ScalarMappable(cmap='viridis', norm=plt.Normalize(vmin=-100, vmax=100))
p1 = f.colorbar(sm, orientation='horizontal', ax=axes[0,0], pad=0.15)
p2 = f.colorbar(sm, orientation='horizontal', ax=axes[1,0], pad=0.15)
p3 = f.colorbar(sm, orientation='horizontal', ax=axes[0,1], pad=0.15)
p4 = f.colorbar(sm, orientation='horizontal', ax=axes[1,1], pad=0.15)

p1.ax.tick_params(labelsize=7)
p2.ax.tick_params(labelsize=7)
p3.ax.tick_params(labelsize=7)
p4.ax.tick_params(labelsize=7)

plt.tight_layout()

plt.show()

enter image description here

0 个答案:

没有答案