- 第二节课作业
题目:
假设大家在30岁的时候,根据自己的实际情况,统计出来了你和同桌从11岁到30岁每年交的男女朋友的数量如列表a和b,请绘制出该数据的折线图,以便分析自己和同桌每年交男女朋友的数量走势
a = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
b = [1,0,3,1,2,2,2,3,1,1,1,1,1,2,1,1,2,3,2,2]
要求:
• y轴表示个数
• x轴表示岁数,比如11岁,12岁
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family':'SimHei',
'weight':'bold',
'size':12
}
matplotlib.rc("font", **font)
plt.figure(figsize=(20,10))
a = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
b = [1,0,3,1,2,2,2,3,1,1,1,1,1,2,1,1,2,3,2,2]
x = list(range(11,31))
x_l = [str(i)+"岁" for i in x]
plt.plot(x,a,label = "自己")
plt.plot(x,b,label = "同桌")
def show_annotate(co):
for i in co:
plt.annotate(str(i[1]),i,i)
co_a = zip(x,a)
co_b = zip(x,b)
show_annotate(co_a)
show_annotate(co_b)
plt.xticks(x,x_l,rotation = 45)
plt.legend()
plt.grid()
plt.xlabel("岁数")
plt.ylabel("个数")
plt.show()
显示效果:
- 第三节课作业
练习一:
为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
价格 | 60 | 80 | 40 | 30 | 70 | 90 | 95 |
---|---|---|---|---|---|---|---|
需求量 | 100 | 50 | 120 | 135 | 65 | 45 | 40 |
price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family':'SimHei',
'weight':'bold',
'size':12
}
matplotlib.rc("font", **font)
x = [60,80,40,30,70,90,95]
y = [100,50,120,135,65,45,40]
x_t = list(range(len(x)))
r = zip(x_t,y)
def show_annotate(r):
for i in r:
plt.annotate(str(i[1]),i,i)
plt.bar(x_t,y)
plt.xlabel("价格")
plt.ylabel("需求量")
plt.xticks(x_t,x)
show_annotate(r)
plt.title("试销实验")
plt.show()
显示效果:
练习二:
电影数据如下:
movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"]
day_12 = [2358,399,2358,362]
day_13 = [12357,156,2045,168]
day_14 = [15746,312,4497,319]
需求:直观体现出不同电影近三天的票房的对比情况
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family':'SimHei',
'weight':'bold',
'size':12
}
matplotlib.rc("font", **font)
movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"]
day_12 = [2358,399,2358,362]
day_13 = [12357,156,2045,168]
day_14 = [15746,312,4497,319]
x = list(range(len(movies_name)))
plt.figure(figsize=(10,10))
new_width = 0.2
x_1 = [i-new_width for i in x]
x_2 = [i+new_width for i in x]
plt.bar(x_1,day_12,width = new_width,label = "12号")
plt.bar(x,day_13,width = new_width,label = "13号")
plt.bar(x_2,day_14,width = new_width,label = "14号")
plt.xticks(x,movies_name)
plt.legend()
def show_annotate(r):
for i in r:
plt.annotate(str(i[1]),i,i)
xy_1 = zip(x_1,day_12)
xy_2 = zip(x,day_13)
xy_3 = zip(x_2,day_14)
show_annotate(xy_1)
show_annotate(xy_2)
show_annotate(xy_3)
plt.xlabel("电影")
plt.ylabel("票房")
plt.title("2020年12日至14日票房")
plt.show()
显示效果
3.第四节课作业
练习一:
题目:绘制班级的身高分布图形
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
from matplotlib import pyplot as plt
import matplotlib
font = {
'family':'SimHei',
'weight':'bold',
'size':12
}
matplotlib.rc("font", **font)
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
x_t = list(range(min(height),max(height),4))
x_l = list(map(lambda x:str(x)+"cm",x_t))
bins = (max(height)-min(height))//4
plt.hist(height,bins=bins,density=True,rwidth=0.8)
plt.xticks(x_t,x_l)
plt.xlabel("身高")
plt.ylabel("频率")
plt.minorticks_on()
plt.show()
显示效果:
练习二:
题目:
实现以下子图布局:
(内容自由发挥)
from matplotlib import pyplot as plt
import matplotlib
font = {
'family':'SimHei',
'weight':'bold',
'size':12
}
matplotlib.rc("font", **font)
import random
fig = plt.figure(figsize=(10,5))
gs = fig.add_gridspec(2,2,width_ratios = (4,1),height_ratios = (1,4))
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[1,0])
ax3 = fig.add_subplot(gs[1,1])
def new_random(axe):
x = list(range(10))
for i in range(10):
y = [random.randint(1, 10) for i in range(10)]
axe.plot(x,y)
new_random(ax1)
new_random(ax2)
new_random(ax3)
plt.show()
显示结果: