Matplotlib学习

Matplotlib基础入门

折线图

# 导入模块
import matplotlib.pyplot as plt
# 处理中文正常显示
plt.rcParams['font.sans-serif']='SimHei'
# 处理 负号 正常显示
plt.rcParams['axes.unicode_minus']=False
# 准备画布  plt.figure( figure=(宽,高))
plt.figure(figsize=(8,4),facecolor='pink')# 8x4英寸,默认一英寸=80dpi
# 画折线图
plt.plot([1,2,3,4],[3,5,4,6])
# 只显示图片
plt.show()

image-20201023112354154

细化折线图

# 细化折线图
# 设置画布大小
plt.figure(figsize=(8,4))
x=[-1,1,3,5]
y=[5,7,1,3]
x1=list(zip(*[(-2,1),(0,4),(2,2),(4,3)]))[0]
y1=list(zip(*[(-2,1),(0,4),(2,2),(4,3)]))[1]

# label=标注
plt.plot(x,y,label='第一条线')
plt.plot(x1,y1,label='第二条线')
#label结合plt.legend() 图例才可以看到
plt.legend()
# xlabel() x轴得标注
plt.xlabel('x轴')
plt.ylabel('y轴')
# 标题
plt.title('图的标题')

image-20201023112604197

Matplotlib进阶

# 导入模块
import matplotlib.pyplot as plt
# 处理中文正常显示
plt.rcParams['font.sans-serif']='SimHei'
# 处理 负号 正常显示
plt.rcParams['axes.unicode_minus']=False

折线图 plt.plot

# 设置画布大小
plt.figure(figsize=(8,4))
x=[-1,1,3,5]
y=[5,7,1,3]
x1=list(zip(*[(-2,1),(0,4),(2,2),(4,3)]))[0]
y1=list(zip(*[(-2,1),(0,4),(2,2),(4,3)]))[1]

# label=标注
plt.plot(x,y,label='第一条线')
plt.plot(x1,y1,label='第二条线',color='pink')
#label结合plt.legend() 图例才可以看到
plt.legend()
# xlabel() x轴得标注
plt.xlabel('x轴')
plt.ylabel('y轴')
# 标题
plt.title('图的标题')

image-20201023112944375

柱状图 plt.bar

print(x,y,x1,y1)
plt.bar(x,y,label='柱状图-01',color='yellow')
# 参数 color=''设置柱子颜色
plt.bar(x1,y1,label='柱状图-02',color='pink')
plt.legend()
# xlabel() x轴得标注
plt.xlabel('x轴')
plt.ylabel('y轴')
# 标题
plt.title('图的标题')

水平柱状图 plt.barth

plt.barh(x,y,label='柱状图-01',color='yellow')
# 参数 color=''设置柱子颜色
plt.barh(x1,y1,label='柱状图-02',color='pink')
plt.legend()
# xlabel() x轴得标注
plt.xlabel('x轴')
plt.ylabel('y轴')
# 标题
plt.title('图的标题')

image-20201023113120082

直方图 plt.hist

import numpy as np
# 假设我们取随机统计2000个人年龄,需要看分析
ages=np.random.randint(18,75,200)
ages

image-20201023113237948

# 准备分箱
bins=np.arange(1,9)*10
bins

image-20201023113307775

list(range(1,9))
# 画直方图
# rwidth='' 柱子的宽度
plt.hist(ages,bins,histtype='bar',label='年龄直方图',rwidth=0.8)
plt.show()

饼图 plt.pie

slices=[2,7,12,3]
activites=['eat','sleep','study','play']
colors=['red','blue','purple','pink']
plt.pie(
    slices,
    labels=activites,  #  显示图例
    colors=colors,#规定颜色
    startangle=45,#开始画的角度
    shadow=True, #是否显示阴影(默认没有)
    explode=(0.1,0,0,0), #突出显示某一块
    autopct='%.2f%%', # 显示保留几位小数的百分比
    labeldistance=1.1, # 图例距离中心的距离,基于半径
    pctdistance=0.6,  # 百分比距离中心的距离,基于半径
    radius=1.5,  #  半径的倍数,实现图的大小
    
)
plt.show()

散点图 plt.scatter

x = [1,2,3,4,5,6,7,8]
y = [5,4,6,3,7,2,9,3]
#  参数 s=40, 点的大小,  marker='*' 点的形状
plt.scatter(x,y,label='散点图',color='#62BC4A',s=80,marker='*')
plt.legend()
plt.show()

image-20201023113625444

柱状图标记

# 准备画布
plt.figure()
# 确定图的位置
# plt.subplot(x,y,z)   ,x行,y列的z位置。 1,1,1
ax1=plt.subplot(1,1,1)
# 准备数据
data=np.array([15,20,18,25])

# 画图且返回对象,4个柱子 在 rect 变量中
rect=ax1.bar([1,2,3,4],data,width=0.5,color='#079460')

for each in rect:
    # 获取x坐标
    x=each.get_x()
    # 获取柱子高度
    height=each.get_height()
    print(x,height)
    # 设置文本 ax1.text(x坐标,y坐标,文本)
    ax1.text(x+0.15,height+0.5,f'{height}万')
# 优化工作
ax1.set_xticks([1,2,3,4])
ax1.set_xticklabels([f'第{i}季度' for i in '一二三四'])
# ax1.set_xlabel 与plt.xlabel 一样功能
ax1.set_ylabel('销量单位:(万)')
ax1.set_ylim(0,30)
# 显示网格
ax1.grid(True)
ax1.set_title('x公司四季度销量')

image-20201023115740623

一个画布画多个图

plt.figure(figsize=(6,6),dpi=80,facecolor='pink')
plt.subplot(2,1,1)
plt.plot([1,2,3],[2,4,3])
plt.title('画布的区域1')

plt.subplot(2,1,2)
plt.scatter([1,2,3],[2,4,3])
plt.title('画布的区域2')

plt.show()

image-20201023115844576

plt.figure(figsize=(8,4),dpi=80,facecolor='pink')
ax2=plt.subplot(1,2,1)
ax2.plot([1,2,3],[2,4,3])
ax2.set_title('画布的区域1')

ax3=plt.subplot(1,2,2)
ax3.scatter([1,2,3],[2,4,3])
ax3.set_title('画布的区域2')

image-20201023115914023

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值