Matplotlib常用绘图指令大全

常用配置: matplotlib.rcParams

本文首发于本csdn博主私人博客:Timing is Fun

  • A dictionary object including validation
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    mpl.rcParams['font.sans-serifs'] = ['SimHei'] # 中文支持
    mpl.rcParams['axes.unicode_minus'] = False # 正常显示负号
    
    mpl.rcParams['lines.linewidth'] = 5 # 线条宽度
    mpl.rcParams['lines.color'] = 'red' # 线条颜色
    mpl.rcParams['lines.linestyle'] = '-' # 线条样式

基本图形

直方图

  • 查看分布规律
  • plt.hist(x, bins)
    • x - array or sequence of (n,) arrays,数据data
    • bins - int or sequence or str, optional, 横座标区间
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    height = [168, 155, 182, 170, 173, 161]
    bins = range(150,191,5)
    
    plt.hist(height, bins=bins)
    plt.show()

在这里插入图片描述

条形图

  • 同类数据进行对比
  • plt.bar(x, y)
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    classes = ['class 1', 'class 2', 'class 3']
    scores = [70, 80, 60]
    
    plt.bar(classes, scores)
    plt.show()

在这里插入图片描述

折线图

  • 展示数据随某一指标变化的关系
  • plt.plot(x, y)
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    plt.plot(year, height)
    plt.title('身高随时间变化图')
    plt.show()

在这里插入图片描述

饼图

  • 展示不同类在整体中所占的比重
  • plt.pie(data, labels, autopct)
    • data - array or sequence of (n,) arrays,数据data
    • labels - list, optional, default: None,数据标签
    • autopct - None (default), string, or function, optional,占比计算
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    labels = ['衣', '食', '住', '行' ]
    data = [2000, 4000, 2000, 1000]
    
    plt.pie(data, labels=labels, autopct='%1.1f%%')
    plt.show()

在这里插入图片描述

散点图

  • 验证二维数据的相关性
  • 发现不同类别的二维数据区位图
  • plt.scatter(X, Y)
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    data = [[18.9, 10.4], [21.3, 8.7], [19.5, 11.6], [20.5, 9.7], [19.9, 9.4], [22.3, 10.6]]
    
    X = [item[0] for item in data]
    Y = [item[1] for item in data]
    
    plt.scatter(X, Y)
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.title('超市商品价位与销量散点图')
    plt.xlabel('价格(元)')
    plt.ylabel('销量(件)')
    plt.text(19, 11, '牙膏')
    plt.text(20, 9.5, '纸巾')
    plt.text(22, 10.5, '洗衣液')

在这里插入图片描述

箱线图

  • 显示一组数据分散情况的统计图

在这里插入图片描述

  • plt.boxplot(X)
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    data=[77, 70, 72, 89, 89, 70, 90, 87, 94, 63, 81, 99, 94, 80, 95, 67, 65, 88, 60, 67]
    plt.boxplot(X)
    plt.show()

在这里插入图片描述

极线图

  • 极座标下的数据图,善于表示周期性数据
    • 数据点: [极径,角度]
  • projection = ‘polar’
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    r = [1,2,3,4,5] #极径
    theta = [0.0, 1.57, 3.14, 4.71, 6.28] # 角度
    
    ax = plt.subplot(111, projection='polar')
    ax.plot(theta, r)

在这里插入图片描述

阶梯图

  • 一般用来反映随时间变化的数据
  • plt.step(X, Y)
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    year = range(2005, 2015)
    height = [157, 160, 162, 163, 167, 170, 173, 175, 179, 182]
    
    plt.step(year, height)

在这里插入图片描述

高级图形

图表参数配置

  • title: 标题
  • xlabel, ylabel: x, y轴标签
  • xticks(x, names): x轴数据标记
    • x - x轴数据
    • names - list, 数据名
  • text(x, y, str): 添加文字
    • x,y - 文字座标
    • str - 添加的文字
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    classes = [1, 2, 3]
    scores = [70, 80, 60]
    names = ['A班', 'B班', 'C班']
    
    plt.bar(classes, scores)
    
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.title('成绩柱状图')
    plt.xlabel('班级')
    plt.ylabel('成绩')
    plt.xticks(classes, names)
    for i in range(1,4):
    	plt.text(i, scores[i-1]+1, scores[i-1])
    plt.show()

在这里插入图片描述

堆积图

  • 展示变量分类趋势和总和趋势
  • plt.bar(x, y, bottom, color, label) - 叠加绘制
    • x, y - 横纵座标
    • bottom - 底部开始值
    • color, label - 颜色,标签
    import matplotlib.pyplot as plt 
    %matplotlib inline
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    n = 5
    number = ['1', '2', '3', '4', '5']
    ch = [72,80,66,77,92]
    math = [62, 92, 72, 75, 88]
    eng = [76, 81, 73, 75, 80]
    chmath = [math[i]+ch[i] for i in range(5)]
    x = range(0,5)
    
    plt.bar(x, ch, color='r', label='语文')
    plt.bar(x, math, bottom=ch, color='g', label='数学')
    plt.bar(x, eng, bottom=chmath, color='b', label='英语')
    plt.ylim(0, 300)
    plt.title('成绩')
    plt.legend(loc='upper right')
    plt.grid(axis='y', color='gray', linestyle=':', linewidth=2)
    plt.xticks(x, number)
    plt.xlabel('学号')

在这里插入图片描述

分块图

  • 对比同一变量不同类别的值大小
  • plt.bar(x, y, width=0.4, fc=‘r’)
    • x,y - 横纵座标
    • width - 宽度
    • fc - 颜色
    import matplotlib.pyplot as plt 
    %matplotlib inline
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    names = ['语', '数', '英']
    c1 = [81.4, 83, 87.1] # class 1
    c2 = [85.6, 87.4, 90] # class 2
    c3 = [78, 81.2, 86.1] # class 3
    
    width = 0.4
    
    x1 = [1,3,5]
    x2 = [1.4,3.4,5.4]
    x3 = [1.8,3.8,5.8]
    
    plt.bar(x1, c1, label='class 1', fc='r', width=width)
    plt.bar(x2, c2, label='class 2', fc='g', width=width)
    plt.bar(x3, c3, label='class 3', fc='b', width=width)
    plt.xticks(x2, names)
    plt.legend()
    plt.title('班级成绩图')
    plt.ylabel('成绩')

在这里插入图片描述

气泡图

  • 散点图基础上增加气泡大小来表示另一个变量,感受三个变量的关系
  • plt.scatter(x, y, s=Z)
    • x, y - 横纵座标
    • s - 气泡大小
    import matplotlib.pyplot as plt 
    %matplotlib inline
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    data = [[18.9, 10.4, 20], [21.3, 8.7, 40], [19.5, 11.6, 60], [20.5, 9.7, 80], \
    [19.9, 9.4, 60], [22.3, 10.6,40]]
    
    X = [item[0] for item in data]
    Y = [item[1] for item in data]
    Z = [item[2] for item in data]
    
    plt.scatter(X, Y, s=Z)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.slabel('z')

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章