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')

在這裏插入圖片描述

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