CASE2.1
绘制折线图
折线图适合表现数据随时间变化的趋势。示例使用 plot() 绘制上半年销售额,通过 marker 参数突出每个数据点,并补充标题与坐标轴名称,使图表具备基本的可读性。
import matplotlib.pyplot as plt
# 准备数据
months = ['1月', '2月', '3月', '4月', '5月', '6月']
sales = [120, 145, 132, 178, 205, 230]
# 创建画布并绘制折线图
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(months, sales, color='#1E3050', linewidth=2, marker='o')
# 设置标题与坐标轴名称
ax.set_title('上半年销售额趋势', fontsize=16)
ax.set_xlabel('月份')
ax.set_ylabel('销售额 / 万元')
plt.tight_layout()
plt.show()
提示 · 替换提示:将运行上述代码得到的图形保存为 assets/images/ch02/fig-02-01.png。