import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# ===== 强制中文字体设置 =====
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
# ========== 1. 读取Excel数据 ==========
file_path = r"D:/趋势图.xlsx"
df = pd.read_excel(file_path, engine='openpyxl')
# 直接读取原始数据(不提取数值)
interval_labels = df["区间"].values # 原始区间标签
area_before = df["作业前面积"].values
area_after = df["作业后面积"].values
# 用序号作为X轴位置
x_positions = np.arange(len(interval_labels))
# ========== 2. 墙线图 ==========
fig, ax = plt.subplots(figsize=(14, 8), dpi=200)
ax.set_facecolor('white')
fig.patch.set_facecolor('white')
# ----- 2.1 作业前:蓝色半透明墙 -----
ax.fill_between(x_positions, 0, area_before,
color='#4a9eff', alpha=0.35, label='作业前面积')
ax.plot(x_positions, area_before,
color='#4a9eff', linewidth=3, marker='o', markersize=4,
markerfacecolor='white', markeredgewidth=1.5, label='_nolegend_')
# ----- 2.2 作业后:橙色半透明墙(叠加)-----
ax.fill_between(x_positions, 0, area_after,
color='#ff6b35', alpha=0.45, label='作业后面积')
ax.plot(x_positions, area_after,
color='#ff6b35', linewidth=3.5, marker='s', markersize=5,
markerfacecolor='white', markeredgewidth=2, label='_nolegend_')
# ----- 2.3 提升区域(填充)-----
ax.fill_between(x_positions, area_before, area_after,
where=(area_after >= area_before),
color='#f1c40f', alpha=0.25, interpolate=True, label='提升区域')
# ========== 3. 坐标轴美化 ==========
ax.set_xlabel('NDVI', fontsize=14, color='black', fontweight='bold')
ax.set_ylabel('面积 (万平方公里)', fontsize=14, color='black', fontweight='bold')
ax.tick_params(colors='black', labelsize=10)
# X轴刻度:替换 -0.0 和双减号
ax.set_xticks(x_positions)
clean_labels = []
for label in interval_labels:
label_str = str(label)
# 把 -0.0 替换成 0
label_str = label_str.replace('-0.0', '0')
# 把 -- 替换成 -(处理 -0.1--0.0 这种情况)
label_str = label_str.replace('--', '-')
clean_labels.append(label_str)
ax.set_xticklabels(clean_labels, rotation=0, ha='center', fontsize=8)
# Y轴刻度(0-50,步长5)
ax.set_ylim(0, 50)
ax.set_yticks(np.arange(0, 51, 5))
# 网格线(浅灰色)
ax.grid(color='#cccccc', linestyle='--', linewidth=0.5, alpha=0.5)
# 边框
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('black')
ax.spines['bottom'].set_color('black')
# ========== 4. 图例 ==========
ax.legend(loc='upper right', fontsize=12, facecolor='white',
edgecolor='black', labelcolor='black', framealpha=0.9)
plt.tight_layout(pad=5.0)
plt.show()