首页 > 解决方案 > 为多个图的所有子图设置颜色条

问题描述

我正在为一年中的每个月制作地理熊猫图(地图),涵盖十年的数据。每年一个图,每个图有 12 个子图。我可以使数字很好,但我想让颜色条在所有子图和数字中保持一致,以便于比较。所有数据都有不同的值,但都在 0 到 10000 之间。我想拉伸每张地图的颜色以匹配这个。

到目前为止,我有:

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

# set the filepath and load in a shapefile
fp = 'Senegal_Grid__all_WGS84.gpkg'
map_df = gpd.read_file(fp, layer="Senegal_Grid__all_WGS84")

# shp for clipping the map_df
Senegal = 'sen_admbnda_adm0_1m_gov_ocha_20190426.shp'

Sen_df = gpd.read_file(Senegal)

map_df = gpd.clip(map_df, Sen_df)

years = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]

for year in years:
    print(year)
    fig, axes = plt.subplots(nrows=6, ncols=2, figsize=(2.5,6.5))

    ax1 = map_df.plot(ax=axes[0, 0], column='modis_jan_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax2 = map_df.plot(ax=axes[1, 0], column='modis_feb_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax3 = map_df.plot(ax=axes[2,0], column='modis_mar_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax4 = map_df.plot(ax=axes[3,0], column='modis_apr_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax5 = map_df.plot(ax=axes[4,0], column='modis_may_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax6 = map_df.plot(ax=axes[5,0], column='modis_jun_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax7 = map_df.plot(ax=axes[0,1], column='modis_jul_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax8 = map_df.plot(ax=axes[1,1], column='modis_aug_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax9 = map_df.plot(ax=axes[2,1], column='modis_sep_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax10 = map_df.plot(ax=axes[3,1], column='modis_oct_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax11 = map_df.plot(ax=axes[4,1], column='modis_nov_mean_' + str(year), cmap = "viridis", linewidth=0.1)
    ax12 = map_df.plot(ax=axes[5,1], column='modis_dec_mean_' + str(year), cmap = "viridis", linewidth=0.1)

    im = plt.gca().get_children()[0]
    cax = fig.add_axes([0.1,0.05,0.8,0.03])

    cmap = matplotlib.cm.viridis
    bounds = [0, 2500, 5000, 7500, 10000]
    norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)

    fig.colorbar(im, cax=cax, orientation='horizontal', ticks=bounds, norm=norm, spacing='uniform')

    plt.title(str(year) + ' MODIS NDVI')
    plt.savefig('modis_NDVI_' + str(year) + '.png', format='PNG')
    plt.clf()

这让我得到了 10 张如下所示的地图:

在此处输入图像描述

在此处输入图像描述

但比例因图而异(参见附图中的颜色条)。我想修复 Colorbar,以便可以更轻松地比较所有 10 个数字。

如何设置子图和图形的比例?

标签: pythonmatplotlibgeopandas

解决方案


推荐阅读