不同子时段作物含水量时间序列趋势计算及可视化需求
分时段作物含水量趋势计算与可视化方案
以下提供R和Python两种实现方式,完成指定时段的趋势计算与可视化:
R实现
1. 数据准备与时段划分
# 加载依赖包 library(lubridate) library(ggplot2) # 创建数据集 dates <- seq(as.Date("2016-09-01"), as.Date("2020-07-30"), by=15) water <- c(0.5702722, 0.5631781, 0.5560839, 0.5555985, 0.5519783, 0.5463459, 0.5511598, 0.546652, 0.5361545, 0.530012, 0.5360571, 0.5396569, 0.5683526, 0.6031535, 0.6417821, 0.671358, 0.7015542, 0.7177007, 0.7103561, 0.7036985, 0.6958607, 0.6775161, 0.6545367, 0.6380155, 0.6113306, 0.5846186, 0.5561815, 0.5251135, 0.5085149, 0.495352, 0.485819, 0.4730029, 0.4686458, 0.4616468, 0.4613918, 0.4615532, 0.4827496, 0.5149105, 0.5447824, 0.5776764, 0.6090217, 0.6297454, 0.6399422, 0.6428941, 0.6586344, 0.6507473, 0.6290631, 0.6011123, 0.5744375, 0.5313527, 0.5008027, 0.4770338, 0.4564025, 0.4464508, 0.4309046, 0.4351668, 0.4490393, 0.4701232, 0.4911582, 0.5162941, 0.5490387, 0.5737573, 0.6031149, 0.6400073, 0.6770058, 0.7048311, 0.7255012, 0.739107, 0.7338938, 0.7265202, 0.6940718, 0.6757214, 0.6460862, 0.6163091, 0.5743775, 0.5450822, 0.5057753, 0.4715266, 0.4469859, 0.4303232, 0.4187793, 0.4119401, 0.4201316, 0.426369, 0.4419331, 0.4757525, 0.5070846, 0.5248457, 0.5607567, 0.5859825, 0.6107531, 0.6201754, 0.6356589, 0.6336177, 0.6275579, 0.6214981) df <- data.frame(date = dates, water_content = water) # 划分目标时段 period1 <- df[df$date >= as.Date("2016-09-01") & df$date <= as.Date("2019-11-30"), ] period2 <- df[df$date >= as.Date("2019-12-15") & df$date <= as.Date("2020-07-30"), ]
2. 计算线性趋势
# 时段1趋势回归 model1 <- lm(water_content ~ as.numeric(date), data = period1) trend1 <- coef(model1) cat("时段1(2016-09-01至2019-11-30)趋势:\n") cat(paste0("斜率:", round(trend1[2], 8), ",截距:", round(trend1[1], 4), "\n")) # 时段2趋势回归 model2 <- lm(water_content ~ as.numeric(date), data = period2) trend2 <- coef(model2) cat("时段2(2019-12-15至2020-07-30)趋势:\n") cat(paste0("斜率:", round(trend2[2], 8), ",截距:", round(trend2[1], 4), "\n"))
3. 可视化绘制
ggplot(df, aes(x = date, y = water_content)) + geom_point(color = "darkblue", size = 1.5) + # 时段1趋势线 geom_smooth(data = period1, method = "lm", se = FALSE, color = "red", linetype = "solid") + # 时段2趋势线 geom_smooth(data = period2, method = "lm", se = FALSE, color = "green", linetype = "solid") + # 时段分割虚线 geom_vline(xintercept = as.Date("2019-11-30"), linetype = "dashed", color = "gray") + # 趋势标注 annotate("text", x = as.Date("2018-03-01"), y = max(df$water_content)*0.95, label = paste0("时段1趋势:斜率=", round(trend1[2], 8)), color = "red") + annotate("text", x = as.Date("2020-02-01"), y = max(df$water_content)*0.9, label = paste0("时段2趋势:斜率=", round(trend2[2], 8)), color = "green") + labs(title = "作物含水量时间序列及分时段趋势", x = "日期", y = "作物含水量") + theme_minimal()
Python实现
1. 数据准备与时段划分
import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # 创建数据集 dates = pd.date_range(start="2016-09-01", end="2020-07-30", freq="15D") water = np.array([0.5702722, 0.5631781, 0.5560839, 0.5555985, 0.5519783, 0.5463459, 0.5511598, 0.546652, 0.5361545, 0.530012, 0.5360571, 0.5396569, 0.5683526, 0.6031535, 0.6417821, 0.671358, 0.7015542, 0.7177007, 0.7103561, 0.7036985, 0.6958607, 0.6775161, 0.6545367, 0.6380155, 0.6113306, 0.5846186, 0.5561815, 0.5251135, 0.5085149, 0.495352, 0.485819, 0.4730029, 0.4686458, 0.4616468, 0.4613918, 0.4615532, 0.4827496, 0.5149105, 0.5447824, 0.5776764, 0.6090217, 0.6297454, 0.6399422, 0.6428941, 0.6586344, 0.6507473, 0.6290631, 0.6011123, 0.5744375, 0.5313527, 0.5008027, 0.4770338, 0.4564025, 0.4464508, 0.4309046, 0.4351668, 0.4490393, 0.4701232, 0.4911582, 0.5162941, 0.5490387, 0.5737573, 0.6031149, 0.6400073, 0.6770058, 0.7048311, 0.7255012, 0.739107, 0.7338938, 0.7265202, 0.6940718, 0.6757214, 0.6460862, 0.6163091, 0.5743775, 0.5450822, 0.5057753, 0.4715266, 0.4469859, 0.4303232, 0.4187793, 0.4119401, 0.4201316, 0.426369, 0.4419331, 0.4757525, 0.5070846, 0.5248457, 0.5607567, 0.5859825, 0.6107531, 0.6201754, 0.6356589, 0.6336177, 0.6275579, 0.6214981]) df = pd.DataFrame({"date": dates, "water_content": water}) # 划分目标时段 period1 = df[(df["date"] >= "2016-09-01") & (df["date"] <= "2019-11-30")] period2 = df[(df["date"] >= "2019-12-15") & (df["date"] <= "2020-07-30")]
2. 计算线性趋势
# 日期转数值(自时段起始日的天数) def date_to_num(df): return (df["date"] - df["date"].min()).dt.days.values.reshape(-1, 1) # 时段1趋势计算 X1 = date_to_num(period1) y1 = period1["water_content"].values model1 = LinearRegression().fit(X1, y1) print(f"时段1(2016-09-01至2019-11-30)趋势:") print(f"斜率:{model1.coef_[0]:.8f},截距:{model1.intercept_:.4f}") # 时段2趋势计算 X2 = date_to_num(period2) y2 = period2["water_content"].values model2 = LinearRegression().fit(X2, y2) print(f"时段2(2019-12-15至2020-07-30)趋势:") print(f"斜率:{model2.coef_[0]:.8f},截距:{model2.intercept_:.4f}")
3. 可视化绘制
plt.figure(figsize=(12, 6)) # 绘制原始观测点 plt.scatter(df["date"], df["water_content"], color="darkblue", s=20, label="观测值") # 绘制时段1趋势线 plt.plot(period1["date"], model1.predict(X1), color="red", linestyle="-", label=f"时段1趋势(斜率={model1.coef_[0]:.8f})") # 绘制时段2趋势线 plt.plot(period2["date"], model2.predict(X2), color="green", linestyle="-", label=f"时段2趋势(斜率={model2.coef_[0]:.8f})") # 添加时段分割线 plt.axvline(pd.to_datetime("2019-11-30"), color="gray", linestyle="--", label="时段分割") plt.title("作物含水量时间序列及分时段趋势", fontsize=14) plt.xlabel("日期", fontsize=12) plt.ylabel("作物含水量", fontsize=12) plt.legend() plt.xticks(rotation=45) plt.tight_layout() plt.show()
内容的提问来源于stack exchange,提问作者kl40
相关产品推荐
相关产品推荐

