python - 为多个图的所有子图设置颜色条
问题描述
我正在为一年中的每个月制作地理熊猫图(地图),涵盖十年的数据。每年一个图,每个图有 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 个数字。
如何设置子图和图形的比例?
解决方案
推荐阅读
- typescript - 将来自其他对象的参数作为已定义类型打字稿中的参数传递。对象字面量只能指定已知属性,ts(2345)
- swift - 如何访问和修改核心数据
- html - 单击“显示更多”按钮后如何对行应用和删除褪色渐变
- swift - 如何在 SwiftUI 视图中进行导航控制
- python - 汇总 df 中的行
- javascript - 如何在单击操作按钮时将选定复选框的值作为连接字符串发送到文本输入中
- c# - C# Slack Bot 到 Slack Channel - 使用 Slash 命令而不是聊天
- javascript - 使用反应钩子单击按钮时道具不更新
- cmake - CMake 用 add_custom_target 中的相对“../../”替换命令的部分绝对路径
- server - isc-dhcp-server.service 失败