2.1
折线图:用 plot() 看趋势
折线图用来表现随时间变化的连续数据,x 轴通常是时间,y 轴是数值。
数据:某店铺 1—6 月销售额(示例数据)。marker='o' 让每个月的数据点显式可见。
python
import matplotlib.pyplot as plt
# 中文与负号显示设置(本页后续示例省略这两行)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
months = ['1月', '2月', '3月', '4月', '5月', '6月']
sales = [120, 132, 101, 134, 90, 160]
plt.figure(figsize=(7.2, 4))
plt.plot(months, sales, marker='o')
plt.title('上半年各月销售额')
plt.xlabel('月份')
plt.ylabel('销售额(万元)')
plt.show()
- plot(x, y) 中 x 可以是字符串列表,Matplotlib 会按顺序等距排列。
- 只传一个列表时,它被当作 y 值,x 自动取 0,1,2…
- marker 控制数据点形状,不设置时只画连线。




