# 2.1.1 使用plot()绘制折线图
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
plt.plot(arr[0], arr[1:])
运行如下
## 2.1.2实例1:未来15天最高气温和最低气温
# 01_maximum_minimum_temperatures
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4, 19)
y_max = np.array([34, 35, 30, 33, 29, 30, 30, 28, 32, 29, 26, 27, 28, 25, 31])
y_min = np.array([17, 15, 18, 20, 21, 20, 19, 18, 17, 18, 17, 18, 19, 17, 17])
# 绘制折线图
plt.plot(x, y_max)
plt.plot(x, y_min)
plt.show()
运行如下
# 2.2 绘制柱形图或堆积柱形图
## 2.2.1 使用bar()绘制柱形图或堆积柱形图
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(5)
y1 = np.array([9, 7, 9, 9, 11])
# 柱形的宽度
bar_width = 0.3
# 绘制柱形图
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.show()
运行如下
x = np.arange(5)
y1 = np.array([9, 7, 9, 9, 11])
y2 = np.array([8, 9, 8, 10, 12])
# 柱形的宽度
bar_width = 0.3
# 根据多组数据绘制柱形图
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.bar(x + bar_width, y2, width=bar_width)
plt.show()
运行如下
# 绘制堆积柱形图
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.bar(x, y2, bottom=y1, width=bar_width)
plt.show()
运行如下
# 偏差数据
error = [2, 1, 2.5, 2, 1.5]
# 绘制带有误差棒的柱形图
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.bar(x, y1, bottom=y1, width=bar_width, yerr=error)
plt.show()