66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
import pandas as pd
|
||
import matplotlib.pyplot as plt
|
||
import numpy as np
|
||
from datetime import datetime
|
||
|
||
# 读取CSV文件
|
||
file_path = r"D:\WQ\zhanghuilai\hyperspectral-inversion\data\input\一代高光谱\2025-07-11_2025-07-21\筛选.csv" # 替换为你的文件路径
|
||
df = pd.read_csv(file_path)
|
||
|
||
# 获取第一列名(时间戳列)
|
||
timestamp_col = df.columns[0]
|
||
|
||
# 解析时间戳列(注意格式:2025/7/7 15:03)
|
||
df[timestamp_col] = pd.to_datetime(df[timestamp_col], format='%Y-%m-%d %H:%M:%S')
|
||
|
||
# 提取日期列(用于分组)
|
||
df['Date'] = df[timestamp_col].dt.date
|
||
|
||
# 获取波长数据(第一行,跳过时间戳列)
|
||
# 尝试将列名转换为浮点数,如果失败则保持原样
|
||
wavelengths = []
|
||
for col in df.columns[1:-1]:
|
||
try:
|
||
wavelengths.append(float(col))
|
||
except ValueError:
|
||
wavelengths.append(col)
|
||
|
||
# 按日期分组计算平均光谱
|
||
daily_avg = df.groupby('Date')[df.columns[1:-1]].mean()
|
||
|
||
# 设置绘图参数
|
||
plt.figure(figsize=(12, 8))
|
||
plt.title('Daily Average Spectral Reflectance', fontsize=16)
|
||
plt.xlabel('Wavelength (nm)', fontsize=14)
|
||
plt.ylabel('Reflectance', fontsize=14)
|
||
plt.grid(True, alpha=0.3)
|
||
|
||
# 为每条曲线生成不同的颜色
|
||
colors = plt.cm.viridis(np.linspace(0, 1, len(daily_avg)))
|
||
|
||
# 绘制每条平均光谱曲线
|
||
for i, (date, row) in enumerate(daily_avg.iterrows()):
|
||
# 将日期格式化为更易读的形式
|
||
formatted_date = date.strftime('%Y-%m-%d')
|
||
|
||
# 绘制光谱曲线
|
||
plt.plot(wavelengths, row.values,
|
||
label=formatted_date,
|
||
color=colors[i],
|
||
linewidth=2)
|
||
|
||
# 添加图例
|
||
plt.legend(title='Date', bbox_to_anchor=(1.05, 1), loc='upper left')
|
||
|
||
# 优化布局
|
||
plt.tight_layout()
|
||
|
||
# 保存图像
|
||
plt.savefig(r'D:\WQ\zhanghuilai\hyperspectral-inversion\data\input\一代高光谱\2025-07-11_2025-07-21\plot\daily_average_spectra.png', dpi=300, bbox_inches='tight')
|
||
|
||
|
||
|
||
# 输出每日平均光谱数据到CSV
|
||
# daily_avg.to_csv('D:\WQ\zhanghuilai\hyperspectral-inversion\data\input\一代高光谱\daily_average_spectra.csv')
|
||
print("每日平均光谱数据已保存到 daily_average_spectra.csv")
|
||
print("光谱图已保存到 daily_average_spectra.png") |