文件
需要学生文件的可以私信我
过程
利用numpy的loadtxt文件读取学号,性别,和身高。
import numpy as np
import matplotlib.pyplot as pltfilename='/Users/oommnn/Desktop/python学习/数据分析/网课资料/第04天/student-data.txt'user_info=np.dtype([('subject','U10'),('sex','U1'),('height','f')])data=np.loadtxt(filename,dtype=user_info,skiprows=9,usecols=(0,1,3),encoding='utf-8')#排序默认升序
#argsort()函数返回数组元素的索引值,这些元素按升序排序。
index_order=data['height'].argsort()
#倒序
index_order=index_order[::-1]
data=data[index_order]
#获得全部学生的体重数据
subjects=data['subject']
heights=data['height']
#分别获得男生和女生的体重数据
isboy=data['sex']=='M'
isgirl=data['sex']=='F'
subjects_boy=data['subject'][isboy]
heights_boy=data['height'][isboy]
subjects_girl=data['subject'][isgirl]
heights_girl=data['height'][isgirl]#figsize设置大小
#subplot_mosaic:将图像分为四个区域,用left和right进行组合,可以将四部分合并也可以分分散
fig, axs = plt.subplot_mosaic([['left_top', 'left_top'],['left', 'right']],figsize=(12,5),layout='constrained',facecolor='lightskyblue')axs['left_top'].scatter(subjects,heights)
axs['left_top'].set_title('Stduent Height',fontsize=15)
axs['left_top'].set_xlabel('student subject',fontsize=12)
axs['left_top'].set_ylabel('height/m')axs['left'].scatter(subjects_boy,heights_boy)
axs['left'].set_title('Boy Height',fontsize=15)
axs['left'].set_xlabel('boy student subject',fontsize=12)
axs['left'].set_ylabel('height/m')axs['right'].scatter(subjects_girl,heights_girl)
axs['right'].set_title('Girl Height',fontsize=15)
axs['right'].set_xlabel('girl student subject',fontsize=12)
axs['right'].set_ylabel('height/m')plt.show()