对一个班级62个同学身高进行了统计,结果如下表所示
150,152,153,153,155,154,156,157,157,158, 158,158,157,159,159,160,160,161,161,160, 159,159,160,160,162,162,163,163,163,164, 164,164,163,162,164,164,163,163,164,163, 165,165,166,166,166,167,167,166,166,167, 168,168,169,170,169,171,172,171,173,174, 175,176 |
请用Matplotlib绘制这个班学生身高的直方图.假设把数据分成9组。
要求:
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd# 获取csv文件数据
dz='D:\itheima\sg.csv'
df =pd.read_csv(dz)
print(df)# 数据内容
X=[150, 153, 156, 159, 162, 165, 168, 171, 174, 177]
Y=np.array(df)
plt.hist(Y, X ,9,edgecolor = 'w',)# 设置X轴的刻度
plt.xticks((150, 153, 156, 159, 162, 165, 168, 171, 174, 177),('150', '153', '156',' 159', '162', '165', '168', '171', '174', '177'))# 用黑体显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 设置x,y,标题
plt.title("身高统计直方图")
plt.ylabel('数量/个')
plt.xlabel('身高/cm')
plt.show()