第3节课Matplotlib作业
- 练习1:
- 练习2:
练习1:
绘制班级的身高分布图形
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
解答:
from matplotlib import pyplot as pltplt.rcParams[\'font.sans-serif\'] = [\'SimHei\'] # 步骤一(替换sans-serif字体)plt.rcParams[\'axes.unicode_minus\'] = False # 步骤二(解决坐标轴负数的负号显示问题)height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]cha = max(height)-min(height)b=5bi=round(cha)//bplt.hist(height,bins = bi,density=True)plt.xlabel(\"区间\")plt.ylabel(\"频率\")plt.title(\"班级身高分布直方图\")plt.show()
练习2:
from matplotlib import pyplot as pltplt.rcParams[\'font.sans-serif\'] = [\'SimHei\'] # 步骤一(替换sans-serif字体)plt.rcParams[\'axes.unicode_minus\'] = False # 步骤二(解决坐标轴负数的负号显示问题)from matplotlib import gridspecimport randomfig = plt.figure()width = (3,1)height = (1,3)gs = fig.add_gridspec(2,2,width_ratios=width,height_ratios=height)# [行,列] [0,0] 取一整行 列取全部ax1 = fig.add_subplot(gs[0,0])ax2 = fig.add_subplot(gs[1,0])ax3 = fig.add_subplot(gs[1,1])ax1.plot(range(5),range(5))ax2.scatter([random.randint(10,30) for i in range(30)],[ random.randint(10,30)for i in range (30)])ax3.plot(range(5),range(5))plt.show()