matplotlib基本要點
目錄- matplotlib基本要點
- 什么是matplotlib
- 為什么要學習matplotlib
- matplotlib基本要點
- 簡單使用:假設一天中每隔兩小時range(2,26,2)的氣溫℃分別是[15,13,14.5,17,20,25,26,26,27,22,18,15]
- 可以改進的地方
- 總結之前我們都做了什么
- 什么是matplotlib
什么是matplotlib
matplotlib:最流行的Python底層繪圖庫,主要做詩句可視化圖表,名字取材于MATLAB,模仿MATLAB構建
為什么要學習matplotlib
- 能將資料進行可視化,更直觀的呈現
- 使資料更加直觀、更具說服力
matplotlib基本要點

- axis軸,指的是x或者y這種坐標軸
- 每個紅色的點是坐標,把五個點的坐標連接成一條直線,組成了一個折線圖
簡單使用:假設一天中每隔兩小時range(2,26,2)的氣溫℃分別是[15,13,14.5,17,20,25,26,26,27,22,18,15]
from matplotlib import pyplot as plt
x = range(2, 26, 2)
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
# 繪圖
plt.plot(x, y)
# 展示圖形
plt.show()

可以改進的地方
- 設定圖片大小(要一個高清無碼大圖)
fig = plt.figure(figsize=(20,8),dpi=80)
figure圖形圖示的意思,在這里指的就是我們畫的圖
通過實體化一個figure并且傳遞引數,能夠在后臺自動使用該figure實體
在影像模糊的時候可以傳入dpi引數,讓圖片更加清晰
- 保存到本地
保存圖片
plt.savefig("./sit_size.png")
可以保存為svg這種矢量圖格式,放大不會有鋸齒
-
描述資訊,比如x軸和y軸表示什么,這個圖表示什么,設定中文字體
- 為什么無法顯示中文:matplotlib默認不支持中文字符,因為默認的英文字體無法顯示漢字
- 查看linux/mac下面支持的字體:
- fc-list -> 查看支持的字體
- fc-list :lang=zh ->查看支持的中文(冒號前面有空格)
- 如何修改matplotlib的默認字體?
- 通過matplotlib.rc可以修改
- 通過matplotlin 下的font_manager可以解決
# 設定中文字體
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
plt.xticks(x[::5],_x_ticks[::5],rotation=90,fontproperties=my_font)
plt.xlabel("時間",fontproperties=myfont)#設定x軸的label
plt.ylabel("溫度(℃)",fontproperties=myfont)#設定y軸的label
plt.title("10點到12點每分鐘的時間變化情況",frontproperties=myfont)#設定title
- 調整x或y的刻度的間距
x = range(2,26,2)
plt.xticks(x[::2])
當刻度太密集時候使用串列的步長(間隔取值)來解決,matplotlib會自動幫我們對應,
- 線條的樣式(比如顏色,透明度等)
繪制網格,alpha為透明度
plt.grid(alpha= 0.4)
繪制自己的圖形風格

| 顏色字符 | 風格字符 |
|---|---|
| r 紅色 | - 實線 |
| g 綠色 | -- 虛線 |
| b 藍色 | -. 點劃線 |
| w 白色 | : 點虛線,虛線 |
| '' 留慷訓空格,無線條 | |
| c 青色 | |
| m 洋紅 | |
| y 黃色 | |
| k 黑色 | |
| #00ff00十六進制 | |
| 0.8 灰度值字串 |
-
標記出特殊的點(比如告訴別人最高點和最低點在哪兒)
-
給圖片添加一個水印(防偽,防止盜用)
- 案例代碼一
"""
matplotlib繪制10點到12點的氣溫
"""
import matplotlib.pyplot as plt
x = range(2, 26, 2)
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
# 設定圖片大小 ,figsize(寬,高),dpi每英寸上點的個數
plt.figure(figsize=(20, 8), dpi=80)
# 設定x軸的刻度 , X軸并不是我們自己的刻度
_xtick_lables = [i / 2 for i in range(4, 49)]
plt.xticks(_xtick_lables[::3]) # 當刻度太密集使用串列的步長(間隔取值)來解決,matplotlib會自動幫我們對應
plt.yticks(range(min(y), max(y) + 1))
# 繪圖
plt.plot(x, y)
# 保存圖片
# plt.savefig("./t1.png")
# 展示圖形
plt.show()

- 案例代碼二
"""
串列a表示10點到12點的每一分鐘的氣溫,如何繪制折線圖觀察每分鐘氣溫的變化情況
a = [random.randint(20,35) for i in range(120)]
"""
from matplotlib import pyplot as plt, font_manager
import random
import matplotlib
# Windows和Linux設定字體的方式
# font = {'family': 'monospace',
# 'weight': 'bold',
# 'size': 'larger'}
# matplotlib.rc()
# matplotlib.rc("font",family='MicroSoft YaHei', weight='bold')
# 另外一種設定字體大小的方式
# my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\mingliub.ttc")
# 設定字體為SimHei顯示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 設定正常顯示字符
plt.rcParams['axes.unicode_minus'] = False
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
# 調整圖片大小
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
# 調整x軸的刻度
_xtick_lables = ["10點{}分".format(i) for i in range(60)]
_xtick_lables += ["11點{}分".format(i) for i in range(60)]
# 取步長,數字和字串一一對應,資料的長度一樣
# plt.xticks(list(x)[::3], _xtick_lables[::3], rotation = 45, fontproperties=my_font) # rotation,旋轉的度數
plt.xticks(list(x)[::3], _xtick_lables[::3], rotation=45)
# 添加描述資訊
plt.xlabel("時間")
plt.ylabel("溫度 單位(℃)")
plt.title("10點到12點每分鐘氣溫變化情況")
plt.show()

- 案例三
"""
假設大家在30歲的時候,根據自己的實際情況,
統計出來了從11歲到30歲每年交的女(男)朋友的數量如串列a,
請繪制出該資料的折線圖,以便分析自己每年交女(男)朋友的數量走勢
a = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y表示個數
x表示歲數,比如11歲,12歲等
"""
from matplotlib import pyplot as plt
y = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
x = range(11, 31)
# 設定中文
# 設定字體為SimHei顯示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 設定正常顯示字符
plt.rcParams['axes.unicode_minus'] = False
# 設定圖形大小
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
# 設定x軸刻度
x_ticks_lables = ["{}歲".format(i) for i in x]
plt.xticks(x, x_ticks_lables)
# 繪制網格,設定透明度
plt.grid(alpha=0.4)
# 展示
plt.show()

- 案例四
"""
假設大家在30歲的時候,根據自己的實際情況,
統計出來了你和你同桌從11歲到30歲每年交的女(男)朋友的數量如串列a和串列b,
請在一個圖中繪制出該資料的折線圖,
以便分析自己和同桌20年之間的差異,同時分析每年交女(男)朋友的數量走勢
a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
b = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
y表示個數
x表示歲數,比如11歲,12歲等
"""
from matplotlib import pyplot as plt
y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_2 = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
x = range(11, 31)
# 設定中文
# 設定字體為SimHei顯示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 設定正常顯示字符
plt.rcParams['axes.unicode_minus'] = False
# 設定圖形大小
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y_1, label="自己", color="orange", linestyle=':')
plt.plot(x, y_2, label="同桌", color="cyan", linestyle='--')
# 設定x軸刻度
x_ticks_lables = ["{}歲".format(i) for i in x]
plt.xticks(x, x_ticks_lables)
# 繪制網格
plt.grid(alpha=0.4)
# 添加圖列
plt.legend(loc="upper left")
# 展示
plt.show()

總結之前我們都做了什么
- 繪制了折線圖(plt.plot)
- 設定了圖片的大小和解析度(plt.figure)
- 實作了圖片的保存(plt.savefig)
- 設定了xy軸上的刻度和字串(xticks)
- 解決了刻度系數和密集的問題(xticks)
- 設定了標題,xy軸的lable(title,xlable,ylable)
- 設定了字體(font_manager.fontProperties,matplotlib.rc)
- 在同一個圖上繪制多個圖形(plt多次plot即可)
- 為不同的圖形添加圖例




轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/274348.html
標籤:其他
上一篇:Java中的四種參考型別
