其他分享
首页 > 其他分享> > 第2节课Matplotlib案例及作业

第2节课Matplotlib案例及作业

作者:互联网

首先,全局引入需要的库和字体设置

import random
from matplotlib import pyplot as plt

import matplotlib
font = {

matplotlib.rc(“font”, **font)

案例1.假设通过爬虫你获取到了长沙2019年4,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温和随时间变化的某种规律

x_4 = range(1,32)
x_10 = range(51,82)
y_4 = [11,17,16,11,12,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]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,
21,20,22,15,11,15,5,13,15,10,11,13,12,13,6]

plt.figure(figsize=(10,8))
plt.scatter(x_4,y_4)
plt.scatter(x_10,y_10)

x_t = list(x_4)+list(x_10)
x_l = [f"四月{i}日" for i in x_4]
x_l += [f"十月{i-50}日" for i in x_10]
plt.xticks(x_t[::3],x_l[::3],rotation=75)
plt.xlabel(“time”)
plt.ylabel(“temp”)
plt.title(“relaxtion”)
plt.show()
在这里插入图片描述

案例2 假设你获取到了2019年内地电影票房前20的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据

plt.figure(figsize=(14,8))
movies_x = [“流浪地球”,“复仇者联盟4:终局之战”,“哪吒之魔童降世”,“疯狂的外星人”,“飞驰人生”, “蜘蛛侠:英雄远征”,“扫毒2天地对决”,“烈火英雄”,“大黄蜂”,“惊奇队长”,“比悲伤更悲伤的故事”, “哥斯拉2:怪兽之王”,“阿丽塔:战斗天使”,“银河补习班”,“狮子王”,“反贪风暴4”,“熊出没”,“大侦探皮卡丘”, “新喜剧之王”,“使徒行者2:谍影行动”,“千与千寻”]
movies_height = [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,6.23,5.22]

plt.bar(movies_x,movies_height,width=0.8,align=“edge”)
x_t = range(len(movies_x))
plt.xticks(x_t,movies_x,rotation=90)
plt.show()
在这里插入图片描述

案例3 绘制柱状图和叠状图

fruits = [“苹果”,“梨子”,“车厘子”]
q1_sales = [1000,800,3000]
q2_sales = [1200,700,2800]

width = 0.35
po_l = [i-width/2 for i in range(len(fruits))]
plt.bar(po_l,q1_sales,width=width,label=“q1_sales”)

po_r = [i+width/2 for i in range(len(fruits))]
plt.bar(po_r,q2_sales,width=width,label=“q2_sales”)
plt.legend()

plt.xticks(range(len(fruits)),fruits)

plt.title(“relaxtions”)
plt.show()
在这里插入图片描述
fruits = [“苹果”,“梨子”,“车厘子”]
q1_sales = [1000,800,3000]
q2_sales = [1200,700,2800]

plt.bar(fruits,q1_sales,width=0.5,label=“q1”)
plt.bar(fruits,q2_sales,width=0.5,label=“q2”,bottom=q1_sales)

plt.legend()
plt.xticks(range(len(fruits)),fruits)

plt.show()
在这里插入图片描述

案例4 根据数据,推断该地区四月份平均气温的分布类型

temp_li= [6.9,4.1,6.6,5.2,6.4,7.9,8.6,3.0,4.4,6.7,7.1,4.7,9.1,6.8,8.6,5.2,5.8,7.9,5.6,8.8,
8.1,5.7,8.4,4.1,6.4,6.2,5.2,6.8,5.6,5.6,6.8,8.2,6.4,4.8,6.9,7.1,9.7,6.4,7.3,6.8,
7.1,4.8,5.8,6.5,5.9,7.3,5.5,7.4,6.2,7.7]

cha=max(temp_li)-min(temp_li)
b=1
bi=int(round(cha)/b)
plt.hist(temp_li,bi,density=True)
plt.show()
在这里插入图片描述

作业2.1 绘制班级里同学们的身高分布图形

height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
cha = max(height)-min(height)
b = 1
bi = int(round(cha)/b)

plt.figure(figsize=(14,8))
plt.hist(height,bi,density=True)
plt.xlabel(“班级里同学们的身高”)
plt.ylabel(“分布情况”)
plt.title(“整体分布情况”)
plt.show()
在这里插入图片描述

作业2.2 绘制不一样的子图矩阵

import numpy as np
import matplotlib.gridspec as gridspec

fig = plt.figure(tight_layout=True)
gs = gridspec.GridSpec(2, 2)

ax = fig.add_subplot(gs[0, :])
ax.plot(np.arange(0, 1e6, 1000))
ax.set_ylabel(‘YLabel0’)
ax.set_xlabel(‘XLabel0’)

for i in range(2):

fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels()

plt.show()
在这里插入图片描述

标签:10,plt,width,sales,Matplotlib,案例,fruits,ax,节课
来源: https://blog.csdn.net/weixin_47096630/article/details/112495456