成果图:
主要步骤:
1. 数据准备:下载Hadley Centre observations datasets的HadSST数据
下载链接:Met Office Hadley Centre observations datasets
2. 图像绘制:绘制时间序列图并设置坐标轴名称、刻度、刻度名称、图例位置、图题、阴影检验线
详细代码:着急的直接拖到最后有完整代码
步骤一:导入库包及图片存储路径并设置中文字体为宋体,西文为新罗马(没有的库包要先下好奥)
# 导入库并设置字体
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 设置西文字体为新罗马字体,中文为宋体,字号为12
from matplotlib import rcParams
config = {
"font.family": 'serif',
"font.size": 12,
"mathtext.fontset": 'stix',
"font.serif": ['SimSun'],
}
rcParams.update(config)
rcParams['axes.unicode_minus']=False
步骤二:读入csv文件及所需数据
datapath = r'H:/00.csdn/01data/'
figpath = r'H:/00.csdn/02fig/'
# 读取csv文件,并选择指定的列
datas = pd.read_csv(datapath+'HadSST.4.0.1.0_annual_SHEM.csv',
usecols=['year', 'anomaly', 'lower_bound_95pct_bias_uncertainty_range', 'upper_bound_95pct_bias_uncertainty_range'])
datan = pd.read_csv(datapath+'HadSST.4.0.1.0_annual_NHEM.csv',
usecols=['year', 'anomaly', 'lower_bound_95pct_bias_uncertainty_range', 'upper_bound_95pct_bias_uncertainty_range'])
datag = pd.read_csv(datapath+'HadSST.4.0.1.0_annual_GLOBE.csv',
usecols=['year', 'anomaly', 'lower_bound_95pct_bias_uncertainty_range', 'upper_bound_95pct_bias_uncertainty_range'])
xg = datag['year']
yg = datag['anomaly']
yg1 = datag['lower_bound_95pct_bias_uncertainty_range']
yg2 = datag['upper_bound_95pct_bias_uncertainty_range']
xs = datas['year']
ys = datas['anomaly']
ys1 = datas['lower_bound_95pct_bias_uncertainty_range']
ys2 = datas['upper_bound_95pct_bias_uncertainty_range']
xn = datan['year']
yn = datan['anomaly']
yn1 = datan['lower_bound_95pct_bias_uncertainty_range']
yn2 = datan['upper_bound_95pct_bias_uncertainty_range']
步骤三:绘制图像主体
#绘制图像
fig = plt.figure(figsize=(15, 8))
ax = fig.add_axes([0.1, 0.6, 0.6, 0.4])
# 绘制海温折线
ax.plot(xg, yg, linewidth=1, label='Global SST Anomalies')
ax.plot(xn, yn, linewidth=1, label='Northern Hemisphere SST Anomalies')
ax.plot(xs, ys, linewidth=1, label='Southern Hemisphere SST Anomalies')
# 绘制填充阴影
ax.fill_between(xg, yg1, yg2, alpha=.5, linewidth=0)
ax.fill_between(xn, yn1, yn2, alpha=.5, linewidth=0)
ax.fill_between(xs, ys1, ys2, alpha=.5, linewidth=0)
步骤四:设置坐标轴及图题相关内容
# 设置坐标轴信息
# xlim,ylim:设置x轴y轴显示得最大最小值
# xticks,yticks: 设置坐标轴刻度,可控制隔几个标注,本图为两个一标
# xticklabels,yticklabels:设置坐标轴刻度名称,维度要和xticks,yticks一致奥
# xlabel,ylabel:坐标轴名称
# title:图名
xlabels = np.arange(1850, 2025, 20)
ylabels = np.arange(-12, 13, 4)/10
ax.set_xticks(xlabels)
ax.set_xticklabels(xlabels, rotation = 0, fontsize = 12)
ax.set_yticks(ylabels)
ax.set_yticklabels(ylabels, fontsize = 12)
ax.set_xlabel('Year',fontsize=12)
ax.set_ylabel('Temperature Anomaly(℃)',fontsize=12)
ax.set_title('Sea Surface Temperature (HadSST4) Anomalies Over the Last 100 Years', fontsize=14)
步骤五:设置图例及水平检验线
# 设置图例
ax.legend(frameon=False, fontsize=12)
# 设置水平检验线
plt.axhline(0, color='grey', linestyle='--')
完整代码在这里哦:
###############################################################################
# 导入库并设置字体
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 设置西文字体为新罗马字体,中文为宋体,字号为12
from matplotlib import rcParams
config = {
"font.family": 'serif',
"font.size": 12,
"mathtext.fontset": 'stix',
"font.serif": ['SimSun'],
}
rcParams.update(config)
rcParams['axes.unicode_minus']=False
###############################################################################
datapath = r'H:/00.csdn/01data/'
figpath = r'H:/00.csdn/02fig/'
# 读取csv文件,并选择指定的列
datas = pd.read_csv(datapath+'HadSST.4.0.1.0_annual_SHEM.csv',
usecols=['year', 'anomaly', 'lower_bound_95pct_bias_uncertainty_range', 'upper_bound_95pct_bias_uncertainty_range'])
datan = pd.read_csv(datapath+'HadSST.4.0.1.0_annual_NHEM.csv',
usecols=['year', 'anomaly', 'lower_bound_95pct_bias_uncertainty_range', 'upper_bound_95pct_bias_uncertainty_range'])
datag = pd.read_csv(datapath+'HadSST.4.0.1.0_annual_GLOBE.csv',
usecols=['year', 'anomaly', 'lower_bound_95pct_bias_uncertainty_range', 'upper_bound_95pct_bias_uncertainty_range'])
xg = datag['year']
yg = datag['anomaly']
yg1 = datag['lower_bound_95pct_bias_uncertainty_range']
yg2 = datag['upper_bound_95pct_bias_uncertainty_range']
xs = datas['year']
ys = datas['anomaly']
ys1 = datas['lower_bound_95pct_bias_uncertainty_range']
ys2 = datas['upper_bound_95pct_bias_uncertainty_range']
xn = datan['year']
yn = datan['anomaly']
yn1 = datan['lower_bound_95pct_bias_uncertainty_range']
yn2 = datan['upper_bound_95pct_bias_uncertainty_range']
###############################################################################
#绘制图像
fig = plt.figure(figsize=(15, 8))
ax = fig.add_axes([0.1, 0.6, 0.6, 0.4])
# 绘制海温折线
ax.plot(xg, yg, linewidth=1, label='Global SST Anomalies')
ax.plot(xn, yn, linewidth=1, label='Northern Hemisphere SST Anomalies')
ax.plot(xs, ys, linewidth=1, label='Southern Hemisphere SST Anomalies')
# 绘制填充阴影
ax.fill_between(xg, yg1, yg2, alpha=.5, linewidth=0)
ax.fill_between(xn, yn1, yn2, alpha=.5, linewidth=0)
ax.fill_between(xs, ys1, ys2, alpha=.5, linewidth=0)
# 设置坐标轴信息
# xticks,yticks: 设置坐标轴刻度,可控制隔几个标注,本图为两个一标
# xticklabels,yticklabels:设置坐标轴刻度名称,维度要和xticks,yticks一致奥
# xlabel,ylabel:坐标轴名称
# title:图名
xlabels = np.arange(1850, 2025, 20)
ylabels = np.arange(-12, 13, 4)/10
ax.set_xticks(xlabels)
ax.set_xticklabels(xlabels, rotation = 0, fontsize = 12)
ax.set_yticks(ylabels)
ax.set_yticklabels(ylabels, fontsize = 12)
ax.set_xlabel('Year',fontsize=12)
ax.set_ylabel('Temperature Anomaly(℃)',fontsize=12)
ax.set_title('Sea Surface Temperature (HadSST4) Anomalies Over the Last 100 Years', fontsize=14)
# 设置图例
ax.legend(frameon=False, fontsize=12)
# 设置水平检验线
plt.axhline(0, color='grey', linestyle='--')
###############################################################################
# 保存图像
plt.savefig(figpath+'01 海温时间序列.png', bbox_inches = 'tight', dpi=600, format='png')
plt.show()