直方图: import matplotlib.pyplot as plt import numpy as np # 1.创建画布 fig = plt.figure() #2. 在画布上添加绘图区域 ax = fig.add_subplot(111) # 3.调用绘图方法绘制图表 # (1)准备数据 data = np.random.randint(0,100,50) ax.hist(data,bins=8,histtype='stepfilled') # (2)展示图表 plt.show()
饼图
# 1.创建画布
fig = plt.figure()
# 2.在画布上添加绘图区域
ax = fig.add_subplot(111)
# 3.准备数据
data = np.array([30, 50, 10, 15, 40, 65])
pie_labels = np.array(['E', 'B', 'C', 'F', 'G', 'H'])
explode_position = [0.05, 0.05, 0.05, 0.05, 0.05, 0.05]
# 4.调用绘图方法绘制图表
ax.pie(data, explode=explode_position, radius=1, labels=pie_labels, autopct='%3.1f%%',shadow=True)
# 5.展示图表
plt.show()
散点图
import matplotlib.pyplot as plt import numpy as np # 1.创建画布 fig = plt.figure() # 2.在画布上添加绘图区域 ax = fig.add_subplot(111) # 3.准备数据 num = 60 x_data = np.random.rand(num) y_data = np.random.rand(num) # 4.调用绘图方法绘制图表 ax.scatter(x_data,y_data) # 5.展示图表 plt.show()
误差棒图:
import matplotlib.pyplot as plt import numpy as np # 1.显示中文 plt.rcParams['font.family'] = 'SimHei' plt.rcParams['axes.unicode_minus'] = False # 2.创建画布 fig = plt.figure() # 3.在画布上添加绘图区域 ax = fig.add_subplot(111) # 4.准备数据 # (1)准备 x 轴和 y 轴的数据 x = np.arange(3) y1 = np.array([3.05, 1.55, 1.22]) y2 = np.array([1.29, 1.61, 1.64]) y3 = np.array([4.35, 4.55, 4.64]) y4 = np.array([3.59, 3.33, 3.20]) # 5.指定测量偏差 error1 = [0.16, 0.08, 0.10] error2 = [0.27, 0.14, 0.14] error3 = [0.34, 0.32, 0.29] error4 = [0.23, 0.23, 0.39] bar_width = 0.2 # 6.调用绘图方法绘制图表 width = 0.2 ax.bar(x, y1,width=bar_width) ax.bar(x+width, y2,align='center',tick_label=['冬季','夏季','秋季'],width=bar_width) ax.bar(x+2*width, y3,width=bar_width) ax.bar(x+3*width, y4,width=bar_width) # 7.添加误差棒 ax.errorbar(x,y1,yerr=error1,capsize=4,capthick=1,fmt='k,') ax.errorbar(x+width,y2,yerr=error2,capsize=4,capthick=1,fmt='k,') ax.errorbar(x+2*width,y3,yerr=error3,capsize=4,capthick=1,fmt='k,') ax.errorbar(x+3*width,y4,yerr=error4,capsize=4,capthick=1,fmt='k,') # 8.展示图表 plt.show()