
这段代码使用Python的Pandas、Numpy和Matplotlib库,对气温数据和健康风险数据进行了可视化分析。代码的主要目的是通过图表展示全球变暖对气温的影响,以及气温变化对健康风险(特别是睡眠呼吸暂停综合征,OSA)的影响。以下是代码的详细介绍:
数据读取
代码首先从CSV文件中读取了两组数据:
wdatempwsalogit设置Matplotlib样式
plt.style.use('nature.mplstyle'):这行代码设置了Matplotlib的绘图样式,使用了nature.mplstyle样式表。这种样式表通常用于科学出版物,能够使图表看起来更加专业和美观。
绘制气温对比图
代码创建了一个包含两个子图的图表:
第一个子图(ax):
t2m_mean)和1950-1990年的历史气温(t2m_historical)随时间(day_of_year)的变化。ax.plot函数绘制了两条曲线,分别表示2023年的气温和历史气温。ax.fill_between函数填充了两条曲线之间的区域,表示全球变暖的影响。第二个子图(ax2):
ax2.plot函数绘制了气温与RR的关系曲线。ax2.fill_between函数填充了置信区间,表示RR的不确定性范围。图表展示
最后,代码使用plt.show()函数展示了绘制的图表。通过这两个子图,可以直观地看到全球变暖对气温的影响,以及气温变化对OSA风险的影响。
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltplt.style.use('nature.mplstyle')wdatemp = pd.read_csv('metadata/figure1a.csv')fig,(ax,ax2)= plt.subplots(1,2)ax.plot(wdatemp['day_of_year'].values, wdatemp['t2m_mean'].values-273.15, label='2023', color='k', alpha=1)ax.plot(wdatemp['day_of_year'].values, wdatemp['t2m_historical'].values, label='1950 to 1990', linestyle='--', color='r', alpha=0.5)y2 = wdatemp['t2m_mean'].values-273.15y1 = wdatemp['t2m_historical'].valuesax.fill_between(x=wdatemp['day_of_year'].values, y1 = y1, y2 = y2, where=y2 >= y1, alpha=0.2, color='k', label='Global warming')ax.set_xticks(np.linspace(0,365,13)[:-1]+15,('Jan',' ','March',' ','May',' ','July',' ','Sep',' ','Nov',' '))ax.grid()ax.legend(loc='upper left')ax.spines.right.set_visible(False)ax.spines.top.set_visible(False)ax.spines.bottom.set_visible(False)ax.spines.left.set_visible(False)ax.tick_params(# changes apply to the x-axis which='both',# both major and minor ticks are affected bottom=False,# ticks along the bottom edge are off top=False,# ticks along the top edge are off left=False,)ax.set_ylabel('Temperature (C)')wsalogit = pd.read_csv('metadata/figure1b.csv')ax2.plot(wsalogit['xvar'].values, wsalogit['est'].values, color='#606c38', label='WSA')ax2.fill_between(wsalogit['xvar'].values, y1=wsalogit['lb'].values, y2=wsalogit['hb'].values, color='#606c38', alpha=0.2)ax2.grid()ax2.legend(loc='upper left')ax2.spines.right.set_visible(False)ax2.spines.top.set_visible(False)ax2.spines.bottom.set_visible(False)ax2.spines.left.set_visible(False)ax2.tick_params(# changes apply to the x-axis which='both',# both major and minor ticks are affected bottom=False,# ticks along the bottom edge are off top=False,# ticks along the top edge are off left=False,)ax2.set_xlabel('Temperature (C)')ax2.set_ylabel('Risk ratio of OSA')plt.show()
这段代码通过Python的Pandas、Numpy和Matplotlib库,对气温数据和健康风险数据进行了可视化分析。代码的主要目的是通过图表展示全球变暖对气温的影响,以及气温变化对健康风险(特别是睡眠呼吸暂停综合征,OSA)的影响。代码分为以下几个主要部分:
nature.mplstyle样式表,使图表看起来更加专业和美观。整体而言,这段代码提供了一个清晰的框架,用于展示气温变化对健康风险的影响,有助于科学地评估全球变暖对人类健康的潜在影响。
通过网盘分享的文件:211.python论文绘图
