matplotlib基础以及常用图示例

matplotlib基础以及常用图示例

导入及配置中文及符号

import matplotlib.pyplot as plt
# %matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

matplotlib中的几种常见图的使用

直方图

直方图可以查看数据的分布情况

height = [168,155,182,170,173,161,155,173,176,181,166,172,175]
bins = range(150,191,5)
# 直方图,可以查看数据的分布情况
plt.hist(height, bins=bins)

直方图

条形图

classes = ['class 1','class 2','class 3']
score = [70,80,60]

# 条形图
plt.bar(classes, score)
plt.show()

条形图

折线图

year = range(2007,2020)
height = [168,155,182,170,173,161,155,173,176,181,166,172,175]
# 折线图
plt.plot(year,height)
plt.show()

折线图

饼图

labels = ['房贷','饮食','出行','教育']
data = [8000,2000,2000,3000]
# 饼图
plt.pie(data, labels=labels, autopct='%1.2f%%')
plt.title('家庭支出饼图')
plt.show()

饼图

散点图

data = [[18.9,10.4],[21.3,8.7],[19.5,11.6],[20.5,9.7],[19.9,9.4],[22.3,11],[21.4,10.6],[9,9.4],[10.4,9],[9.3,11.3],[11.6,8.5],[11.8,10.4],[10.3,10],[8.7,9.5],[14.3,17.2],[14.1,15.5],[14,16.5],[16.5,17.7],[15.1,17.3],[16.4,15],[15.7,18]]
# X,Y
X = [item[0] for item in data]
Y = [item[1] for item in data]
# 散点图
plt.scatter(X,Y)
plt.title('超市商品价位与销量散点图')
plt.xlabel('价格(元)')
plt.ylabel('销量(件)')
plt.text(16,16,'牙膏')
plt.text(10,12,'纸巾')
plt.text(20,10,'洗衣液')
plt.show()

散点图

箱线图

data=[77,70,72,89,89,70,90,87,94,63,81,99,94,80,95,67,65,88,60,67,85,88,87,75,62,65,95,62,61,93,30]
# 箱线图
plt.boxplot(data)
plt.show()

箱线图

极线图

# 极径和角度
r = [1,2,3,4,5] #极径
theta=[0.0,1.5707963267948966,3.141592653589793,4.71238898038469,6.283185307179586] #角度
# 极线图
ax = plt.subplot(111,projection='polar')
ax.plot(theta,r)

极线图

阶梯图

year=range(2005,2020)
height=[157,160,162,163,167,170,176,175,174,179,182,182,182,182.182,183]

# 阶梯图
plt.step(year,height)

阶梯图

带标签的柱状图

x = [1,2,3]
name = ['A班','B班','C班']
y = [80,85,75]

# 条形图
plt.bar(x, y)
plt.title('三班成绩柱状图')
plt.xlabel('班级')
plt.ylabel('成绩')
plt.xticks(x,name)
for i in range(1,4):
    plt.text(i,y[i-1]+1,y[i-1]) # text(x坐标,y坐标,值)
plt.show()

带标签的柱状图

堆积图

# 堆积图
ch=[72,80,66,77,92]
math=[62,92,72,75,88]
eng=[76,81,73,75,80]
plt.bar(range(1,6),ch,color='r')
plt.bar(range(1,6),math,bottom=ch,color='g')
chmath=[ch[i]+math[i] for i in range(5)]
plt.bar(range(1,6),eng,bottom=chmath,color='b')
plt.legend(labels=['chinese','math','english'])
plt.title('分数堆积图')
plt.ylabel('总分')
plt.xlabel('学生')
plt.xticks(range(1,6),['张三','李四','王五','赵六','李七'])

堆积图

分块图

# 分块图
name_list = ['语文','数学','英语']
c1 = [81.4,83,87.1]
c2 = [85.6,87.4,90]
c3 = [78,81.2,86.1]

width = 0.4
x = [1,3,5]

plt.bar(x, c1, label='class 1', fc='r', width=width)

x = [1.4,3.4,5.4]
plt.bar(x, c2, label='class 2', fc='g', width=width)

x = [1.8,3.8,5.8]
plt.bar(x, c3, label='class 3', fc='b', width=width)

x = [1.4,3.4,5.4]
plt.xticks(x,name_list)
plt.legend()
plt.title('三班级成绩图')
plt.xlabel('科目')
plt.ylabel('成绩')

分块图

气泡图

# 气泡图
x=[22,22,23,24,25,25,26,27,28,29,30,30,32,32,32,33,34,34,35,36,37,38,38,39,40,42,43,43,45,45,46,48,48,48,50,52,56,57,60,62]
y=[176,186,164,177,183,194,180,179,190,170,168,192,173,178,181,186,177,187,180,195,179,186,187,190,182,184,176,178,164,185,181,175,173,172,172,169,168,182,188,174]
z=[70, 220, 50, 170, 210, 270, 150, 150, 360, 150, 150, 200, 150, 170, 170, 160, 180, 460, 480, 480, 490, 300, 300, 250, 300, 250, 350, 180, 100, 250, 160, 170, 160, 180, 150, 150, 130, 180, 100, 160]
plt.title('分数堆积图')
plt.scatter(x,y,z)

气泡图

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值