python模块专题——4.matplotlib

27 篇文章 1 订阅
12 篇文章 1 订阅

matplotlib用于数据可视化,熟悉matlab的朋友对他应该不会陌生。

1.简介

数据的处理、分析和可视化已经成为Python近年来最为重要的应用领域之一,其中数据的可视化指的是将数据呈现为漂亮的统计图表,然后进一步发现数据中包含的规律以及隐藏的信息。
在这里插入图片描述

2.安装与使用

  • 安装:使用pip命令进行安装:pip install matplotlib

  • 使用

    from matplotlib import pyplot as plt
    
    x=[x for x in range(1,10)]
    y=[i**2 for i in x]
    plt.plot(x,y)
    plt.show()
    

在这里插入图片描述

3.参数详解

  • 图片大小(绘图函数前设置):fig=plt.figure(figsize=(20,8),dpi=80)

  • 图片保存(绘图函数后设置):plt.savefig("路径")

  • 标题:plt.title("")

  • 坐标轴范围:
    plt.xlim([a,b])
    plt.ylim([a,b])

  • 坐标轴标记

    plt.xlabel("")

    plt.ylabel("")

  • 坐标轴刻度

    plt.xticks(x)

    plt.yticks(y)

  • 字符串刻度

    xtick=["{}".format(i) for i in range(x)]

    plt.xticks(x,xtick)

    :若刻度不需要全部显示,在设置xticks时,可以通过将列表切片xtick[::h]设定步长实现部分显示,此时需要将x做同样的切片

    x为指定刻度,xtick指定刻度上的文字

    plt.xticks(x[::h],xtick[::h])

  • 坐标轴刻度旋转:plt.xticks(rotation=45)

  • 坐标刻度显示中文
    在这里插入图片描述

    from matplotlib import font_manager
    my_font=font_manager.FontProperties(fname="C:/Windows/Fonts/msyh.ttc")
    在需要显示中文的方法(xlabel,xticks,title...)中添加缺省参数:fontproperties=my_font
    在图例中(legend)添加缺省参数:prop=myfont
    

    注意:图例的中文字体属性用prop,其余均用fontproperties

  • 网格:plt.grid(alpha=0.5,linestyle='',color=,linewidth=,axis=)alpha为缺省参数

  • 文本注释:plt.text(x,y,'',fontsize=)

  • 刻度尺显示:plt.tick_params(bottom='on/off',left=,right=)

  • 刻度方向:
    matplotlib.rcParams['xtick.direction']=in/out/inout
    matplotlib.rcParams['ytick.direction']=in/out/inout

  • 图例

    plot(x,y,label="")

    plt.legend([prop=myfont,loc="upper left"])

    同一个图中画多条曲线,直接多次plot即可,同时在plot中的label参数指定图例信息

    图例中文字体属性prop,其他中文属性fontproperties

    图例位置loc:upper/lower left/right/center

  • 绘图函数的缺省参数

    label=""图例信息

    color="r"线条颜色

    linestyle=’–'线形

    linewidth=5线条粗细

    alpha=0.5透明度

    颜色字符线型字符
    r红色c青色-实线
    g绿色m洋红色–虚线、破折线
    b蓝色y黄色-.点划线
    w白色k黑色:虚线、点虚线
    ''留空或空格,无线条
    # coding=utf-8
    from matplotlib import pyplot as plt
    from matplotlib import  font_manager
    
    x=[x for x in range(1,12)]
    y=[i**2 for i in x]
    y2=[100/i for i in x]
    
    # 中文字体
    myfont=font_manager.FontProperties(fname="C:/Windows/Fonts/msyh.ttc")
    
    plt.figure(dpi=80,figsize=(10,8))
    # plt.title("fig1.1")
    plt.title("这是一个中文标题",fontproperties=myfont)
    plt.xlabel('x')
    xtick=["{}m".format(i) for i in x]
    plt.xticks(x[::2],xtick[::2])
    plt.xticks(rotation=45)
    plt.ylabel('y')
    plt.grid(alpha=0.5)
    
    plt.plot(x,y,label='x^2的图像',color='r',linestyle='--',linewidth=5,alpha=0.6)
    plt.plot(x,y2,label='100/x的图像')
    plt.legend(prop=myfont,loc="upper center")
    # plt.savefig("plt.jpg")
    plt.show()
    

在这里插入图片描述

  • 点大小:参数markersize=10

  • 点型: 在dataframe直接调用plot时可使用或者使用pyplot绘图均可使用
    marker=.,ov^<>1234sp*hH+xDd|_

    符号线型
    +十字
    像素点
    o实心圆
    v下三角
    ^上三角
    >右三角
    <左三角
    1tri_down
    2tri_up
    3tri_left
    4tri_right
    s正方形
    p正五边形
    *五角星
    h纵向正六边形
    H横向正六边形
    x
    D纵向正方形
    d菱形
    |竖线
    _横线
  • dataframe直接绘图
    df.plot(kind=‘line/bar/kde/barh/...,rot=,figsize=,title,grid,legend,xticks=,yticks=,xlim=,ylim=,fontsize=,colormap=,label=’)

4.常用统计图

  • 参数调整同plot

    import matplotlib.pyplot as plt
    
    x=[i for i in range(10)]
    y=[j**2 for j in x]
    
    plt.fun(x,y)
    plt.show()
    
  • 折线图

    • plot
      在这里插入图片描述
  • 散点图

    • scatter
      在这里插入图片描述
  • 纵向条形图

    • 离散数据

    • plt.bar(x,y,width=0.4,facecolor='yellowgreen',edgecolor='white',yerr=y*0.1)

    • 绘制多个条形图通过多次bar即可,但是需要将偏移width,xtick和x都需要调整

    • m*n列的数据适合绘制条形图,类内和类间均可对比

    • yerr可以设置错误缓冲区,类似qq图

      import matplotlib.pyplot as plt
      
      x=[i for i in range(1,6)]
      label=["G{}".format(i) for i in x]
      width=0.2
      men_x=[i-width for i in x]
      women_x=x
      other_x=[i+width for i in x]
      men_means = [20, 34, 30, 35, 27]
      women_means = [25, 32, 34, 20, 25]
      other_means=[23, 22, 44, 40, 35]
      
      plt.bar(men_x,men_means,width=width,label="men")
      plt.bar(women_x,women_means,width=width,label="women")
      plt.bar(other_x,other_means,width=width,label="other")
      plt.xticks(x,label)
      plt.legend()
      
      plt.show()
      

      在这里插入图片描述

  • 横向条形图

    • 离散数据,统计数据

    • plt.barh(x,y,height=0.3)

      import matplotlib.pyplot as plt
      height=0.2
      x=[i for i in range(3)]
      year=[i for i in range(2017,2020)]
      print(year)
      beijing=[57768,59868,59906]
      shanghai=[50017,49446,50420]
      guangzhou=[22205,22188,22055]
      
      plt.barh([i-height for i in x],beijing,height=height,label="beijing")
      plt.barh(x,shanghai,height=height,label="shanghai")
      plt.barh([i+height for i in x],guangzhou,height=height,label="guangzhou")
      plt.yticks(range(len(year)),year)
      plt.legend()
      plt.grid(alpha=0.5)
      
      plt.show()
      

      在这里插入图片描述

  • 直方图

    • 连续数据,原始数据

    • 频数直方图:plt.hist(data,n)

    • 频率直方图:plt.hist(data,n,normed=True)

    • 堆叠直方图:plt.hist(data,n,stacked=True)

      # coding=utf-8
      
      '''
      Author:Regent Wan
      Email:wanrunjun@gmail.com
      微信公众号:学术创客
      Github:https://github.com/RegentWan
      
      Date:2019/8/29 11:19
      Desc:
      '''
      
      import matplotlib.pyplot as plt
      
      # 直方图
      time=[131,98,125,131,124,139,131,117,128,108,135,138,131,102,107,114,119,128,121,142,127,130,124,101,116,117,110,128,128,115,99,136,126,134,95,138,117,111,78,132,124,113,150,110,117,86,95,144,105,126,130,126,130,126,116,123,106,112,138,123,86,101,99,136,123,117,119,105,137,123,128,125,104,109,134,125,127,105,120,107,129,116,108,132,103,136,118,102,120,114,105,115,132,145,119,121,112,139,125,138,109,132,134,156,106,117,127,144,139,139,119,140,83,110,102,123,107,143,115,136,118,139,123,112,118,125,109,119,133,112,114,122,109,106,123,116,131,127,115,118,112,135,115,146,137,116,103,144,83,123,111,110,111,100,154,136,100,118,119,133,134,106,129,126,110,111,109,141,120,117,106,149,122,122,110,118,127,121,114,125,126,114,140,103,130,141,117,106,114,121,114,133,137,92,121,112,146,97,137,105,98,117,112,81,97,139,113,134,106,144,110,137,137,111,104,117,100,111,101,110,105,129,137,112,120,113,133,112,83,94,146,133,101,131,116,111,84,137,115,122,106,144,109,123,116,111,111,133,150]
      binWidth=3
      binNum=int((max(time)-min(time))/binWidth)
      plt.hist(time,binNum)
      plt.xticks((range(min(time),max(time)+binWidth))[::binWidth],rotation=30)
      plt.grid(linestyle="-.",alpha=0.5)
      
      plt.show()
      

      在这里插入图片描述

  • 饼图

    • pie(size,explode=,labels=,startangle=)

    • 参数:各部分比例、explode突出部分、label各部分标记、startangle起始角度

      import matplotlib.pyplot as plt
      
      labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
      sizes = [15, 30, 45, 10]
      explode = (0, 0.1, 0, 0)
      plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
      
      plt.show()
      

      在这里插入图片描述

  • 子图

    • 用plt.subplot(row, col, num)来指定子图位置

    • 各子图样式分别设置

      import matplotlib.pyplot as plt
      
      data=[57768,59868,59906]
      plt.subplot(1,2,1)
      plt.plot(range(len(data)),data)
      
      plt.subplot(1,2,2)
      plt.bar(range(len(data)),data)
      
      plt.show()
      

      在这里插入图片描述

  • 子图创建

fig=plt.figure(figsize(10,6),facecolor='gray')
ax1=fig.add_subplot(2,2,1)
plt.plot(x,y)
ax2=fig.add_subplot(2,2,2)
plt.plot(x,y)
  • 填充图
    plt.fill(x,y)
  • 函数间填充图
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,5,100)
y1=np.sin(x)
y2=np.cos(x)
plt.fill_between(x,y1,y2)
plt.show()

在这里插入图片描述

  • 等高线图
  • 散点矩阵图:使用pandas的scatter_matrix函数
import pandas as pd
import matplotlib.pyplot as plt
x=np.random.randn(100,4)
pd.scatter_matrix(pd.DataFrame(x),marker='o',algha=0.5,diagonal='hist')#diagonal=hist/kde
plt.show()

在这里插入图片描述

  • 箱型图:通过dataframe的plot.box或者boxplot绘制
    • vert参数设置是否垂直
    • grid设置网格
x=np.random.randn(10,4)
df=pd.DataFrame(x)
df.plot.box()
#df.boxplot()
plt.show()

在这里插入图片描述

  • 更多图形参考https://matplotlib.org/gallery/index.html

Notice

应博友的要求,创建了一个QQ群,方便大家学习交流,群内也会经常分享一下学习资源。有兴趣的小伙伴可以加群哦!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Regent Wan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值