1.编写程序,使用NumPy模块中的linespace()函数生成[0,10]范围内的100个值,然后计算y=2x+k(其中,k是0~0.5之间的随机数),并绘制(x, y)的散点图。
import matplotlib.pyplot as plt
import numpy as np
k=np.linspace(0,0.5,1)
x=np.linspace(0,10,100)
y=2*x+k
plt.scatter(x,y)
plt.show()
2.变位词是指将某个词或者句子的字母位置(顺序)加以改换所形成的新词,例如python和typhon、heart和earth、triangle和integral等互为变位词。编写程序,从键盘接收两个字符串,使用计数的方法判断这两个字符串是否是变位词。也就是统计两个字符串中每个字母出现的次数,判断相应字母出现的次数是否相同,如果相同,即为变位词,否则不是变位词(忽视大小写)。同时,把一个figure分割成两行一列,在上下两个子图中,用垂直柱状图绘制两个字符串中每个字母出现的次数,要求显示x和y轴刻度标签,x和y轴坐标标题并显示图表题。
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei']
string1=input("please input the first string:")
string2=input("please input the second string:")
c=0
L1=[]
L2=[]
for i in range(0,len(string1)):
if string1.count(string1[i])==string2.count(string1[i]):
c+=1
L1.append(string1.count(string1[i]))
for i in range(0,len(string2)):
L2.append(string2.count(string2[i]))
if c==len(string1):
print('yes')
else:
print('no')
x=[]
for s in string1:
x.append(s)
y=L1
a=[]
for s in string2:
a.append(s)
b=L2
fig=plt.figure()
ax1=fig.add_subplot(2,1,1)
ax1.bar(x,y)
plt.xlabel('字符')
plt.ylabel('出现的次数')
ax1.set_title('第一个字符串中每个字符串出现的次数')
ax2=fig.add_subplot(2,1,2)
ax2.bar(a,b)
plt.xlabel('字符')
plt.ylabel('出现的次数')
ax2.set_title('第二个字符串中每个字符串出现的次数')
plt.show()
3.假设当前目录下有一个文件名为score1.txt的文本文件,存放着某班学生《计算机导论》、《C语言》、《面向对象程序设计》三科成绩,共有学生学号、计算机导论成绩、C语言成绩和面向对象程序设计成绩四列,每行之间采用空格分割。编写程序完成下列功能::
(1)统计每门课90以上、80-89、70-79、60-69、60分以下成绩档学生人数;
(2)把一个figure分割成一行三列,以饼状图的形式显示三门课的分档成绩,要求显示标签百分比和标题等信息。
def getCount(s):
s0=[0,0,0,0,0]
for i in range(len(s)):
if s[i] > 90:
s0[0] += 1
if 80 <= s[i] <= 90:
s0[1] += 1
if 70 < s[i] <= 79:
s0[2] += 1
if 60 <= s[i] <=69:
s0[3] += 1
if s[i] < 60:
s0[4] += 1
return s0
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
nameHandle=open('score1', 'r')
L1=[]
L2=[]
string=''
for line in nameHandle:
for s in line:
if s.isspace() is False:
string+=s
else:
L2.append(string)
string = ''
L2.append(string[:-1])
new_L = list(filter(None, L2))
L1.extend([new_L])
L2=[]
sco1=[]
sco2=[]
sco3=[]
for l in L1:
sco1.append(int(l[1]))
sco2.append(int(l[2]))
sco3.append(int(l[3]))
print(sco1)
print(sco2)
print(sco3)
s1=getCount(sco1)
print(s1)
s2=getCount(sco2)
print(s2)
s3=getCount(sco3)
print(s3)
fig=plt.figure()
ax1=fig.add_subplot(1,3,1)
percentages=[s1[0]/len(sco1),s1[1]/len(sco1),s1[2]/len(sco1),s1[3]/len(sco1),s1[4]/len(sco1)]
plt.pie(percentages,autopct='%1.1f%%')
plt.title("计算机导论")
ax2=fig.add_subplot(1,3,2)
percentages=[s2[0]/len(sco2),s2[1]/len(sco2),s2[2]/len(sco2),s2[3]/len(sco2),s2[4]/len(sco2)]
plt.pie(percentages,autopct='%1.1f%%')
plt.title("C语言")
ax3=fig.add_subplot(1,3,3)
percentages=[s3[0]/len(sco3),s3[1]/len(sco3),s3[2]/len(sco3),s3[3]/len(sco3),s3[4]/len(sco3)]
plt.pie(percentages,autopct='%1.1f%%')
plt.title("面向对象程序设计")
plt.show()
4.老王的血压有些高,医生让家属给老王测量血压。老王的女儿记录了一段时间的血压测量值,在文件xueyajilu.txt文件里,内容实例如下:
2024/5/5 6:00 140,82,136,90,69
2024/5/5 15:50 154,88,155,85,63
2024/5/6 6:30 131,82,139,74,61
2024/5/6 16:47 145,84,139,85,73
2024/5/7 5:03 152,87,131,85,63
文件内各部分含义如下:测量时间,左臂高压,左臂低压,右臂高压,右臂低压,心率
编写程序实现下面功能:
(1)使用字典和列表类型进行数据分析,获取老王的左臂和右臂血压情况5个项目(高压最大值、低压最大值、压差平均值、高压平均值和低压平均值)的对比表,以柱状图的形式进行显示;
(2)以柱状图的形式显示心率的最大值、最小值和平均值。
def getCount(S):
x=[]
str=''
for s in S:
if s==',' :
x.append(int(str))
str=''
else:
str+=s
x.append(int(str))
return x
def getMax(L):
max=L[0]
for l in L:
if l>max:
max=l
return max
def getave(L):
sum=0
for l in L:
sum+=l
return sum/len(L)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
nameHandle=open('xueyajilu', 'r')
L1=[]
L2=[]
string=''
for line in nameHandle:
for s in line:
if s.isspace() is False:
string+=s
else:
L2.append(string)
string = ''
L2.append(string[:-1])
new_L = list(filter(None, L2))
L1.extend([new_L])
L2=[]
x0=getCount(L1[0][2])
x1=getCount(L1[1][2])
x2=getCount(L1[2][2])
x3=getCount(L1[3][2])
x4=getCount(L1[4][2])
x=[x0,x1,x2,x3,x4]
LefHighpressure=[]
LefLowpressure=[]
rigHighpressure=[]
rigLowpressure=[]
heartrate=[]
for i in range(len(L1)):
LefHighpressure.append(x[i][0])
LefLowpressure.append(x[i][1])
rigHighpressure.append(x[i][2])
rigLowpressure.append(x[i][3])
heartrate.append(x[i][4])
lhipre=getMax(LefHighpressure)
llopre=getMax(LefLowpressure)
rhipre=getMax(rigHighpressure)
rlowpre=getMax(rigLowpressure)
lhipreave=getave(LefHighpressure)
llopreave=getave(LefLowpressure)
rhipreave=getave(rigHighpressure)
rlowpreave=getave(rigLowpressure)
heartrateave=getave(heartrate)
heartrate.sort()
minheartrate=heartrate[0]
maxheartrate=heartrate[4]
lefave=0.0
rigave=0.0
sum1=0
sum2=0
for i in range(len(L1)):
sum1+=x[i][0]-x[i][1]
sum2 += x[i][2] - x[i][3]
lefave=sum1/len(L1)
rigave=sum2/len(L1)
print('左臂血压高压最大值为:{},低压最大值为:{},压差平均值为:{},高压平均值为:{},低压平均值为:{}'.format(lhipre,llopre,lefave,lhipreave,llopreave))
print('右臂血压高压最大值为:{},低压最大值为:{},压差平均值为:{},高压平均值为:{},低压平均值为:{}'.format(rhipre,rlowpre,rigave,rhipreave,rlowpreave))
print('心率的最大值为:{},最小值为:{},平均值为:{}'.format(maxheartrate,minheartrate,heartrateave))
m=[1,2,3,4,5]
n=[lhipre,llopre,lefave,lhipreave,llopreave]
m_label=['高压最大值','低压最大值','压差平均值','高压平均值','低压平均值']
fig=plt.figure()
ax1=fig.add_subplot(1,3,1)
plt.xticks(m,m_label,fontsize=8)
ax1.bar(m,n)
ax1.set_title('左臂血压情况')
a=[1,2,3,4,5]
b=[rhipre,rlowpre,rigave,rhipreave,rlowpreave]
a_label=['高压最大值','低压最大值','压差平均值','高压平均值','低压平均值']
ax2=fig.add_subplot(1,3,2)
plt.xticks(a,a_label,fontsize=8)
ax2.bar(a,b)
ax2.set_title('右臂血压情况')
p=[1,2,3]
p_label=['心率最大值','心率最小值','心率平均值']
q=[maxheartrate,minheartrate,heartrateave]
ax3=fig.add_subplot(1,3,3)
plt.xticks(p,p_label,fontsize=8)
ax3.bar(p,q)
ax3.set_title('心率情况')
plt.show()