如何用Python绘制带阴影差值区的气温时间序列对比图?
气温时间序列对比曲线实现方案
Python 实现方法
使用matplotlib库的fill_between函数可快速实现两条曲线间的差值阴影填充,以下是完整示例:
步骤1:准备数据
假设数据已整理为对应两个情景的气温数组及时间序列:
import matplotlib.pyplot as plt import pandas as pd # 生成5年共60个月的时间序列(替换为你的真实时间数据) time = pd.date_range(start='2018-01', periods=60, freq='M') # 0℃变暖情景月最高气温(替换为你的真实数据) temp_0c = [25 + 5*((i%12)/6 - 0.5) + 0.3*pd.np.random.randn() for i in range(60)] # 1℃变暖情景月最高气温(替换为你的真实数据) temp_1c = [t + 1 + 0.3*pd.np.random.randn() for t in temp_0c]
步骤2:绘制对比曲线与差值填充
plt.figure(figsize=(12, 6)) # 绘制两条气温曲线 plt.plot(time, temp_0c, label='0℃变暖情景', color='#1f77b4', linewidth=2) plt.plot(time, temp_1c, label='1℃变暖情景', color='#ff7f0e', linewidth=2) # 填充两条曲线间的差值区域 plt.fill_between(time, temp_0c, temp_1c, color='gray', alpha=0.3, label='气温差值') # 图表美化 plt.xlabel('时间', fontsize=12) plt.ylabel('月最高气温(℃)', fontsize=12) plt.title('不同变暖情景下月最高气温对比', fontsize=14) plt.legend(fontsize=10) plt.grid(alpha=0.3) plt.xticks(rotation=45) plt.tight_layout() plt.show()
核心函数fill_between会自动计算曲线差值并填充区域,alpha参数可调整阴影透明度。
Google Earth Engine (GEE) 实现方案
若数据存储在GEE中,可通过ui.Chart绘制对比曲线,差值阴影需通过叠加面积图模拟:
// 替换为你的0℃、1℃情景气温数据集ID var temp_0c = ee.ImageCollection('...') .select('tasmax') .filterDate('2018-01-01', '2023-01-01') .map(img => img.set('month', img.date().get('month'))); var temp_1c = ee.ImageCollection('...') .select('tasmax') .filterDate('2018-01-01', '2023-01-01') .map(img => img.set('month', img.date().get('month'))); // 计算目标区域的月平均气温(替换为你的区域geometry) var getRegionMean = col => col.map(img => { var mean = img.reduceRegion({ reducer: ee.Reducer.mean(), geometry: geometry, scale: 10000 }); return img.set('temp', mean.get('tasmax')); }); var temp_0c_mean = getRegionMean(temp_0c); var temp_1c_mean = getRegionMean(temp_1c); // 绘制对比折线图 var chart = ui.Chart.feature.groups( temp_0c_mean.merge(temp_1c_mean), 'month', 'temp', 'system:index' ) .setChartType('LineChart') .setOptions({ title: '不同变暖情景下月最高气温对比', hAxis: {title: '月份'}, vAxis: {title: '月最高气温(℃)'}, series: { 0: {color: '#1f77b4', lineWidth: 2}, 1: {color: '#ff7f0e', lineWidth: 2} }, legend: {position: 'top'} }); print(chart);
注:GEE原生图表不支持直接填充曲线区域,需额外计算差值并通过面积图叠加实现阴影效果。
相关参考问题
- 如何使用Matplotlib填充两条折线之间的区域?
- 如何在Google Earth Engine中提取区域平均气候时间序列?
- 如何对长时间序列气候数据进行月度聚合分析?
内容的提问来源于stack exchange,提问作者Vijay Raj Singh
相关产品推荐
相关产品推荐

