求助:如何绘制横轴为患者温度、纵轴为百分比的密度曲线?
如何绘制横轴为患者温度、纵轴为百分比的密度曲线
我需要绘制一张横轴为患者温度、纵轴为百分比的曲线图,目前已有Matplotlib代码,但希望更新成类似密度曲线的样式(纵轴为百分比)。
现有Matplotlib代码
import matplotlib.pyplot as plt df_2022 = df[df['year'] == 2022] df_2023 = df[df['year'] == 2023] total_patients_2022 = len(df_2022) total_patients_2023 = len(df_2023) percentage_2022 = (df_2022['Temperature'].value_counts() / total_patients_2022) * 100 percentage_2023 = (df_2023['Temperature'].value_counts() / total_patients_2023) * 100 percentage_2022 = percentage_2022.sort_index() percentage_2023 = percentage_2023.sort_index() plt.figure(figsize=(12, 6)) plt.plot(percentage_2022.index, percentage_2022.values, label='2022', linestyle='-', color='blue', linewidth=2) plt.plot(percentage_2023.index, percentage_2023.values, label='2023', linestyle='--', color='green', linewidth=2) plt.legend() plt.show()
尝试过的Seaborn密度图代码(纵轴为密度而非百分比)
import seaborn as sns import matplotlib.pyplot as plt df_2022 = df[df['year'] == 2022] df_2023 = df[df['year'] == 2023] sns.kdeplot(data=df_2022['Temperature'], label='2022', shade=True) sns.kdeplot(data=df_2023['Temperature'], label='2023', shade=True) plt.xlabel('Temperature (°C)') plt.ylabel('Density') plt.title('Density Plot of Temperature for 2022 and 2023') plt.legend() plt.show()
数据示例
2022年各温度对应的患者百分比
32.7 2.040816 34.5 2.040816 34.7 2.040816 34.8 4.081633 35.0 2.040816 35.1 2.040816 35.2 2.040816 35.3 6.122449 35.4 2.040816 35.5 16.326531 35.6 6.122449 35.7 2.040816 35.8 8.163265 35.9 6.122449 36.0 12.244898 36.1 2.040816 36.2 2.040816 36.4 2.040816 36.5 6.122449 36.7 4.081633 36.8 2.040816 36.9 4.081633 37.4 2.040816 Name: Temperature, dtype: float64
2023年各温度对应的患者百分比
34.3 2.040816 34.5 2.040816 34.7 2.040816 34.8 2.040816 34.9 2.040816 35.0 6.122449 35.1 2.040816 35.2 2.040816 35.3 4.081633 35.5 8.163265 35.6 2.040816 35.7 2.040816 35.8 2.040816 35.9 4.081633 36.0 10.204082 36.1 8.163265 36.2 6.122449 36.4 6.122449 36.5 8.163265 36.6 6.122449 36.8 6.122449 36.9 4.081633 37.2 2.040816 Name: Temperature, dtype: float64
我需要修改代码,实现横轴为患者温度、纵轴为百分比的密度曲线。
解决方案
要实现纵轴为百分比的密度曲线,有两种实用思路:一是转换Seaborn KDE图的密度值为百分比,二是基于已有百分比数据生成平滑曲线。
方法1:修改Seaborn KDE图,将纵轴转为百分比
直接利用Seaborn的KDE功能,仅转换纵轴刻度为百分比(密度值×100),代码如下:
import seaborn as sns import matplotlib.pyplot as plt df_2022 = df[df['year'] == 2022] df_2023 = df[df['year'] == 2023] plt.figure(figsize=(12, 6)) # 绘制带填充的KDE曲线 sns.kdeplot(data=df_2022['Temperature'], label='2022', shade=True, color='blue', alpha=0.3) sns.kdeplot(data=df_2023['Temperature'], label='2023', shade=True, color='green', alpha=0.3) # 将密度刻度转换为百分比 ax = plt.gca() y_ticks = ax.get_yticks() ax.set_yticklabels(['{:.1f}%'.format(y * 100) for y in y_ticks]) ax.set_ylabel('Percentage') plt.xlabel('Temperature (°C)') plt.title('Density Plot of Temperature (Percentage) for 2022 and 2023') plt.legend() plt.show()
方法2:基于已有百分比数据生成平滑密度曲线
如果要严格基于你已计算好的百分比统计数据生成平滑曲线,可以用scipy.stats.gaussian_kde实现:
import matplotlib.pyplot as plt from scipy.stats import gaussian_kde import numpy as np df_2022 = df[df['year'] == 2022] df_2023 = df[df['year'] == 2023] total_patients_2022 = len(df_2022) total_patients_2023 = len(df_2023) percentage_2022 = (df_2022['Temperature'].value_counts() / total_patients_2022) * 100 percentage_2023 = (df_2023['Temperature'].value_counts() / total_patients_2023) * 100 percentage_2022 = percentage_2022.sort_index() percentage_2023 = percentage_2023.sort_index() # 生成平滑的横轴范围 x_min = min(percentage_2022.index.min(), percentage_2023.index.min()) x_max = max(percentage_2022.index.max(), percentage_2023.index.max()) x_smooth = np.linspace(x_min, x_max, 200) # 对百分比数据做核密度估计平滑 kde_2022 = gaussian_kde(percentage_2022.index, weights=percentage_2022.values, bw_method=0.3) y_smooth_2022 = kde_2022(x_smooth) # 调整曲线高度匹配原百分比的最大值 y_smooth_2022 = y_smooth_2022 / y_smooth_2022.max() * percentage_2022.values.max() kde_2023 = gaussian_kde(percentage_2023.index, weights=percentage_2023.values, bw_method=0.3) y_smooth_2023 = kde_2023(x_smooth) y_smooth_2023 = y_smooth_2023 / y_smooth_2023.max() * percentage_2023.values.max() plt.figure(figsize=(12, 6)) plt.plot(x_smooth, y_smooth_2022, label='2022', linestyle='-', color='blue', linewidth=2) plt.plot(x_smooth, y_smooth_2023, label='2023', linestyle='--', color='green', linewidth=2) # 添加填充效果 plt.fill_between(x_smooth, y_smooth_2022, alpha=0.2, color='blue') plt.fill_between(x_smooth, y_smooth_2023, alpha=0.2, color='green') plt.xlabel('Temperature (°C)') plt.ylabel('Percentage') plt.title('Smoothed Percentage Curve of Temperature for 2022 and 2023') plt.legend() plt.show()
选择说明
- 方法1更简洁高效,适合快速生成符合要求的密度曲线,样式和标准KDE图一致。
- 方法2完全基于你已有统计的百分比数据,可通过调整
bw_method参数控制曲线平滑度,适合需要严格贴合原始统计结果的场景。
内容的提问来源于stack exchange,提问作者tamo007
相关产品推荐
相关产品推荐

