import matplotlib.pyplot as plt
import numpy as np

# ================= 全局配置 (解决中文乱码与负号显示问题) =================
plt.rcParams['font.sans-serif'] = ['SimHei']   # 设置中文显示为黑体
plt.rcParams['axes.unicode_minus'] = False     # 解决负号'-'显示为方块的问题
plt.rcParams['figure.dpi'] = 100               # 设置默认分辨率

# =======================================================================
# 练习一：绘制自定义样式的折线图并导出图片
# =======================================================================
print("正在生成练习一的折线图...")
# 1. 准备数据
x1 = ['1号', '2号', '3号', '4号', '5号', '6号', '7号']
y1 = [123, 232, 244, 321, 330, 420, 553]

# 2. 创建画布并绘图
plt.figure(figsize=(8, 5))
plt.plot(x1, y1, color='blue', linestyle='-', marker='o', markersize=6, label='每日数值')

# 3. 添加标题和标签
plt.title('练习一：每日数据趋势图', fontsize=14)
plt.xlabel('日期', fontsize=12)
plt.ylabel('数值', fontsize=12)
plt.legend()  # 显示图例
plt.grid(True, linestyle='--', alpha=0.5) # 添加网格

# 4. 导出图片
plt.savefig('exercise1_line_chart.png', dpi=300, bbox_inches='tight')
plt.show()


# =======================================================================
# 练习二：使用 Numpy 绘制 Sin(x) 函数折线图并导出图片
# =======================================================================
print("正在生成练习二的 Sin(x) 函数图...")
# 1. 使用 numpy 生成横坐标数据 (-1 到 1，间距 0.1)
x2 = np.arange(-1, 1.1, 0.1)
# 2. 使用 sin(x) 函数生成纵坐标数据
y2 = np.sin(x2)

# 3. 创建画布并绘图
plt.figure(figsize=(8, 5))
plt.plot(x2, y2, color='red', linestyle='--', marker='s', label='sin(x)')

# 4. 添加标题和标签
plt.title('练习二：Sin(x) 函数曲线', fontsize=14)
plt.xlabel('X轴', fontsize=12)
plt.ylabel('Y轴', fontsize=12)
plt.legend()
plt.grid(True, linestyle=':', alpha=0.6)

# 5. 导出图片
plt.savefig('exercise2_sin_chart.png', dpi=300, bbox_inches='tight')
plt.show()


# =======================================================================
# 练习三：绘制自定义样式的柱状图并导出图片
# =======================================================================
print("正在生成练习三的柱状图...")
# 1. 准备数据
x3 = ['2020年', '2021年', '2022年', '2023年', '2024年', '2025年', '2026年']
y3 = [3120, 4420, 5231, 5550, 6123, 6674, 6991]

# 2. 创建画布并绘图
plt.figure(figsize=(9, 5))
bars = plt.bar(x3, y3, color='skyblue', edgecolor='black', width=0.6)

# 在柱状图上方添加具体数值标签
for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2, height + 50, f'{height}', ha='center', va='bottom', fontsize=10)

# 3. 添加标题和标签
plt.title('练习三：年度数据统计', fontsize=14)
plt.xlabel('年份', fontsize=12)
plt.ylabel('总量', fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.5)

# 4. 导出图片
plt.savefig('exercise3_bar_chart.png', dpi=300, bbox_inches='tight')
plt.show()


# =======================================================================
# 练习四：绘制自定义样式的饼图并导出图片
# =======================================================================
print("正在生成练习四的饼图...")
# 1. 准备数据
labels = ['白菜', '大葱', '芹菜', '土豆']
sizes = [10, 25, 25, 40]
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99'] # 自定义颜色
explode = (0, 0, 0, 0.1) # 将"土豆"这一块稍微分离出来

# 2. 创建画布并绘图
plt.figure(figsize=(7, 7))
plt.pie(sizes, labels=labels, colors=colors, explode=explode, 
        autopct='%1.1f%%', startangle=140, shadow=True, wedgeprops={'edgecolor': 'black'})

# 3. 添加标题
plt.title('练习四：蔬菜占比分布', fontsize=14)
plt.axis('equal') # 保证饼图是正圆

# 4. 导出图片
plt.savefig('exercise4_pie_chart.png', dpi=300, bbox_inches='tight')
plt.show()

print("所有图表均已生成并保存至当前目录！")