數據分析————Matplotlib

Matplotlib

什麼是 matplotlib

matplotlib是最流行的Python底層繪畫庫,主要用於數據可視化圖表。
matplotlib可以將數據進行可視化,更直觀的呈現,使數據更加客觀,更加具有說服力。

Matplotlib 常用設置

設置圖片大小
plt.figure(figsize=(20, 8), dpi=100)

dpi參數可以使圖片更加清晰

調整 x 軸或者 y 軸上的刻度
plt.xticks(x[:])
plt.yticks(range(min(y), max(y)+1))
plt.xlabel("時間", fontproperties=myfont)
plt.ylabel("溫度(攝氏度)", fontproperties=myfont)
plt.title("10點到12點每分鐘時間的時間變化情況", fontproperties=titlefont)
plt.show()

通過ticks來設置刻度,label來設置含義

設置中文顯示

在這裏插入圖片描述

#  fc-list  :lang=zh  =====> 列出電腦上所有的中文字體庫
myfont = font_manager.FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=12)
titlefont = font_manager.FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=32, weight=True)

散點圖

import random
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
"""
繪製10月和三月溫度變化的散點圖
"""

# 字體設置
myfont = FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=14)
titlefont = FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=25)

# x軸和Y軸數據
march_y = [random.randint(10, 20) for i in range(31)]
october_y = [random.randint(20, 30) for j in range(31)]
x = range(1, 32) # 1, 2, 3, ...31


# 設置圖形大小
plt.figure(figsize=(10, 5), dpi=100)

# 繪製散點圖
plt.subplot(2,  1,  1)
plt.scatter(x, march_y, )
# 調整刻度
_x3_labels = ["3月{}日".format(_) for _ in x]
# rotation: 旋轉
plt.xticks(x[::3], labels=_x3_labels[::3], fontproperties=myfont, rotation=45)
# x, y和標題的說明
plt.xlabel("月份", fontproperties=myfont)
plt.ylabel("溫度(攝氏度)", fontproperties=myfont)
plt.title("3月溫度變化散點圖", fontproperties=titlefont)

plt.subplot(2,  1,  2)
plt.scatter(x, october_y)
_x10_labels = ["10月{}日".format(_) for _ in x]
plt.xticks(x[::3], labels=_x10_labels[::3], fontproperties=myfont, rotation=45)
# x, y和標題的說明
plt.xlabel("月份", fontproperties=myfont)
plt.ylabel("溫度(攝氏度)", fontproperties=myfont)
plt.title("10月溫度變化散點圖", fontproperties=titlefont)

# 添加網格
plt.grid(alpha=0.5)

# 顯示圖像
plt.show()

散點圖的應用場景
在這裏插入圖片描述

條形圖

單個條形圖繪製
"""
bar(): pyplot 子模塊提供 bar() 函數來生成條形圖。
"""

from matplotlib import pyplot as plt

x = [5, 8, 10]
y = [12, 16, 6]
x2 = [6, 9, 11]
y2 = [6, 15, 7]
plt.bar(x, y, align='center')
plt.bar(x2, y2, color='g', align='center')

plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
多個條形圖繪製
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
df = pd.read_csv('douban.csv')
# 獲取評分的區間
counts = {}
scores = df['score']
for score in scores:
    try:
        # 'score'
        score = float(score.strip())
    except Exception as e:
        continue
    if 9.5 < score <= 10:
        counts['9.5~10'] = counts.get('9.5~10', 0) + 1
    elif 9.0 < score <= 9.5:
        counts['9.0~9.5'] = counts.get('9.0~9.5', 0) + 1
    elif 8.7 < score <= 9.0:
        counts['8.7~9.0'] = counts.get('8.7~9.0', 0) + 1
    elif 8.5 < score <= 8.7:
        counts['8.5~8.7'] = counts.get('8.5~8.7', 0) + 1
    else:
        counts['other'] = counts.get('other', 0)
plt.bar(counts.keys(), counts.values(), color='g')
plt.show()

繪製直方圖

import random
import matplotlib.pyplot as plt

names = ['電影%s' %(i) for i in range(100)]
times = [random.randint(40, 140) for i in range(100)]


# hist代表直方圖, times爲需要統計的數據, 20爲統計的組數.
plt.hist(times, 20)

plt.show()

Matplotlib 拓展知識

Matplotlib 常見問題總結

在這裏插入圖片描述

Matplotlib 使用的流程總結

在這裏插入圖片描述

Matplotlib 更多繪圖樣式

在這裏插入圖片描述

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