You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何绘制DataFrame中p95_annual列温度数据的折线图?

正确绘制气温数据的方法

问题根源

你使用plt.plot(test.p95_annual.values)时,仅传递了气温数值数组,matplotlib会默认用从0开始的连续整数作为x轴坐标。如果你的DataFrame索引是年份(比如1951到2020),或者数据存在索引错位、缺失值,就会导致折线混乱,无法体现年份与气温的对应关系。

解决方案

方法1:直接传入DataFrame列(自动关联年份索引)

如果test的索引是年份(如1951、1952...),直接传入列对象即可,matplotlib会自动将索引作为x轴:

import matplotlib.pyplot as plt

plt.plot(test['p95_annual'])
plt.xlabel('年份')
plt.ylabel('p95_annual气温')
plt.title('1951年以来冬季p95_annual气温变化')
plt.show()

方法2:显式指定年份作为x轴

如果DataFrame中有单独的年份列(例如列名为year),可以明确指定x轴和y轴数据:

plt.plot(test['year'], test['p95_annual'])
plt.xlabel('年份')
plt.ylabel('p95_annual气温')
plt.title('1951年以来冬季p95_annual气温变化')
plt.show()

方法3:处理缺失数据(若存在)

如果数据中包含缺失值(NaN),折线会出现断裂或异常跳转,可先清理数据:

# 删除含缺失值的行
clean_data = test['p95_annual'].dropna()
plt.plot(clean_data)

# 或用前向填充法补全缺失值
filled_data = test['p95_annual'].ffill()
plt.plot(filled_data)

plt.xlabel('年份')
plt.ylabel('p95_annual气温')
plt.show()

内容的提问来源于stack exchange,提问作者Megan Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:16:08