数据分析作业2
直方图(Histogram)
- 直方图介绍:
– 直方图,又称质量分布图,是一种统计报告图,由一系列高度不等的纵向条纹或线段表示数据分布的情况。 一般用横轴表示数据类型,纵轴表示分布情况。 - python绘制直方图:
– 通过hist()函数绘制。 - 练习1:绘制班级的身高分布图形
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
from matplotlib import pyplot as plt
# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
# 计算组数
# 组数 = 极差/组距
difference = max(height)-min(height)
# 绘制直方图
plt.hist(height,density=True)
# 设置轴标签
plt.xlabel("身高")
plt.ylabel("比例")
plt.show()
Axis容器
-
Axis代表的是x轴或者y轴的对象。包含Tick(刻度)对象,TickLabel刻度文本对象,以及AxisLabel坐标轴文本对象。axis对象有一些方法可以操作刻度和文本等。
-
练习2:实现以下子图布局:
from matplotlib import pyplot as plt
import random
fig = plt.figure()
# 设定子图宽高比
width=[3,1]
height=[1,3]
# 添加子图
gs = fig.add_gridspec(2,2,width_ratios=width,height_ratios=height)
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[1,0])
ax3 = fig.add_subplot(gs[1,1])
ax1.plot([0,4],[0,3])
x_list = [random.randint(10,30) for i in range(30)]
y_list = [random.randint(10,35) for i in range(30)]
ax2.scatter(y_list,x_list)
ax3.plot([0,3],[0,4])
plt.show()