Files
hyperspectral-hongshengrese…/chl-a含量图.py
2025-07-22 10:27:31 +08:00

44 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 1. 设置中文字体SimHei 或自行替换路径)
font_path = 'C:/Windows/Fonts/simhei.ttf' # 适用于 Windows
zh_font = font_manager.FontProperties(fname=font_path)
plt.rcParams['font.sans-serif'] = [zh_font.get_name()]
plt.rcParams['axes.unicode_minus'] = False # 解决负号乱码
# 2. 读取 CSV 数据
file_path = r"D:\WQ\zhanghuilai\hyperspectral-inversion\data\input\一代高光谱\output\chla画图.csv" # ← 替换为你的实际文件路径
df = pd.read_csv(file_path)
# 3. 预处理数据
df.columns = ['timestamp', 'chl_a']
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['date'] = df['timestamp'].dt.date # 只保留年月日
# 4. 计算每日平均和标准差
daily_stats = df.groupby('date')['chl_a'].agg(['mean', 'std']).reset_index()
# 5. 绘图
fig, axs = plt.subplots(2, 1, figsize=(10, 8))
# 图 1每日 chl-a 浓度柱状图(含误差线)
axs[0].bar(daily_stats['date'], daily_stats['mean'], yerr=daily_stats['std'],
capsize=4, color='skyblue', edgecolor='black')
axs[0].set_title('每日 chl-a 浓度均值(含标准差)', fontproperties=zh_font)
axs[0].set_xlabel('日期', fontproperties=zh_font)
axs[0].set_ylabel('chl-a 浓度', fontproperties=zh_font)
axs[0].tick_params(axis='x', rotation=45)
# 图 2chl-a 浓度分布直方图
axs[1].hist(df['chl_a'], bins=30, color='lightgreen', edgecolor='black')
axs[1].set_title('chl-a 浓度分布直方图', fontproperties=zh_font)
axs[1].set_xlabel('chl-a 浓度', fontproperties=zh_font)
axs[1].set_ylabel('频数', fontproperties=zh_font)
# 6. 调整布局并保存图像
plt.tight_layout()
plt.savefig('D:\WQ\zhanghuilai\hyperspectral-inversion\data\input\一代高光谱\output\chl_a_图表.png', dpi=300, bbox_inches='tight') # 高分辨率保存
plt.show()