例:统计甲乙两人从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]
def getAnnotate(paramter1,paramter2): #将两个可迭代对象列表打包成一个zip对象,并且设置xy交点处数据
for p1,p2 in zip(paramter1,paramter2):
plt.annotate(f"{(p2,p1)}",xy=(p1,p2),xytext=(p1,p2-0.15))
a_y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
b_y = [1,0,3,1,2,2,2,3,1,1,1,1,1,2,1,1,2,3,2,2]
age_x = range(11,31)
plt.figure(figsize=(14,8)) #设置图大小
plt.plot(age_x,a_y,label="甲",marker="*") #设置x,y轴,设置标签与xy交点
plt.plot(age_x,b_y,label="乙",marker="*")
plt.legend() #使标签生效
f_x = ["{}岁".format(i) for i in age_x] #获取刻度值
plt.xticks(age_x,f_x,rotation=45) #设置刻度,设置刻度值,设置刻度值倾角
getAnnotate(age_x,a_y) #调函数
getAnnotate(age_x,b_y)
plt.title("甲乙两人交友走势图") #标题
plt.show() #显示图片并释放内存
以下例子不在写备注,仅供参考…
散点图
某地2019年4月与10月份每天白天的最高气温(分别为列表c,d),那么此时如何寻找出气温和随时间变化的某种规律: c = [11,17,16,11,12,13,10,14,8,13,12,15,14,17,18,21,16,17,30,14,15,15,15,19,21,22,22,22,23] d = [26,26,28,19,21,17,16,19,18,20,20,49,22,23,17,20,21,20,22,15,11,15,2,13,15,10,11,13,12,13,6]
c = [11,17,16,11,12,13,10,14,8,13,12,15,14,17,18,21,16,17,30,14,15,15,15,19,21,22,22,22,23] # 29
d = [26,26,28,19,21,17,16,19,18,20,20,29,22,23,17,20,21,20,22,15,11,15,2,13,15,10,11,13,12,13,6] # 31
c_x = range(1,30)
d_x = range(1,32)
plt.figure(figsize=(14,8))
plt.scatter(c_x, c, label="四月")
plt.scatter(d_x, d, label="十月")
plt.legend()
month_x = list(c_x) + list(d_x)
cc_y = ["四月{}号".format(i) for i in c_x]
dd_y = ["十月{}号".format(i-50) for i in d_x]
cd_y = cc_y + dd_y
plt.xticks(month_x[::3],cd_y[::3],rotation=45)
plt.show()
条形图
假如2019年内地电影票房前20的电影(列表e)和电影票房数据(列表f),那么如何更加直观的展示数据? e = [“流浪地球”,“复仇联盟4”,“哪吒之魔童降世”,“疯狂的外星人”,“飞驰人生”,“蜘蛛侠:英雄远征”,“扫毒2天地对决”,“烈火英雄”,“大黄蜂”,“惊奇队长”,“比悲伤更悲伤的故事”,“哥斯拉2怪兽之王”,“阿丽塔:战斗天使”,“银河补习班”,“狮子王”,“反贪风暴4”,“熊出没”,“大侦探皮卡丘”,“新喜剧之王”,“使徒行者”,“千与千寻”] f = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88] 单位:亿
e = ["流浪地球","复仇联盟4","哪吒之魔童降世","疯狂的外星人","飞驰人生","蜘蛛侠:英雄远征","扫毒2天地对决","烈火英雄","大黄蜂","惊奇队长","比悲伤更悲伤的故事","哥斯拉2怪兽之王","阿丽塔:战斗天使","银河补习班","狮子王","反贪风暴4","熊出没","大侦探皮卡丘","新喜剧之王","使徒行者"]
f = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,5.22]
plt.figure(figsize=(14,8))
plt.bar(e,f,width=0.8,bottom=10,align="center")#默认width=0.8,align有两个值:center与edge(根据width调整)
plt.xticks(rotation=90)
plt.show()
分组条形图
plt.figure(figsize=(14,8))
fruits = ["苹果","梨子","香蕉"]
q1_sales = [1000,800,3000]
q2_sales = [1200,700,2800]
width = 0.35
#r1 = plt.bar(list(range(len(fruits))),q1_sales,width)
#r2 = plt.bar(list(range(len(fruits))),q2_sales,width)
one = [i-width/2 for i in list(range(len(fruits)))]
r1 = plt.bar(one,q1_sales,width,label="q1")
two = [i+width/2 for i in list(range(len(fruits)))]
r2 = plt.bar(two,q2_sales,width,label="q2")
plt.legend()
plt.xticks(list(range(len(fruits))),fruits)
plt.show()
e = ["流浪地球","复仇联盟4","哪吒之魔童降世","疯狂的外星人","飞驰人生","蜘蛛侠:英雄远征","扫毒2天地对决","烈火英雄","大黄蜂","惊奇队长","比悲伤更悲伤的故事","哥斯拉2怪兽之王","阿丽塔:战斗天使","银河补习班","狮子王","反贪风暴4","熊出没","大侦探皮卡丘","新喜剧之王","使徒行者"]
f = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,5.22]
plt.figure(figsize=(14,8))
plt.barh(e,f)#可以通过hight修改
plt.show()