python中常用的兩個畫圖庫是Matplotlib和Seaborn局雄,直接pip安裝即可。
常玩吃雞的小伙伴應(yīng)該很熟悉雷達圖存炮,游戲結(jié)束后會出現(xiàn)一張評價雷達圖炬搭,我們可以用Matplotlib庫來制作
雷達圖
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
labels = np.array([u" 生存 ", u" 傷害 ", u" 擊敗 ", u" 支援 ", u" 物資 "])
stats = [100.0, 95.0, 95.8, 62.8, 96.8]
angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats = np.concatenate((stats, [stats[0]]))
angles = np.concatenate((angles, [angles[0]]))
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, stats, 'o-', linewidth=2)
ax.fill(angles, stats, alpha=0.25)
# 設(shè)置中文字體
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
ax.set_thetagrids(angles * 180/np.pi, labels, FontProperties=font)
plt.show()
成對關(guān)系
我們用 Seaborn 自帶的 iris 數(shù)據(jù)集,什么是 Iris 數(shù)據(jù)集呢穆桂?這里引用下百度百科的解釋:Iris 數(shù)據(jù)集是常用的分類實驗數(shù)據(jù)集宫盔,由 Fisher, 1936 收集整理。Iris 也稱鳶尾花卉數(shù)據(jù)集享完,是一類多重變量分析的數(shù)據(jù)集灼芭。數(shù)據(jù)集包含 150 個數(shù)據(jù)集,分為 3 類般又,每類 50 個數(shù)據(jù)彼绷,每個數(shù)據(jù)包含 4 個屬性≤钋ǎ可通過花萼長度寄悯,花萼寬度,花瓣長度堕义,花瓣寬度4個屬性預(yù)測鳶尾花卉屬于(Setosa猜旬,Versicolour,Virginica)三個種類中的哪一類胳螟。
import matplotlib.pyplot as plt
import seaborn as sn
iris = sn.load_dataset('iris')
sn.pairplot(iris)
plt.show()
熱力圖
熱力圖是以特殊高亮的形式顯示訪客熱衷的頁面區(qū)域和訪客所在的地理區(qū)域的圖示昔馋。
數(shù)據(jù)用 seaborn 中自帶的 flights 航班數(shù)據(jù),記錄了 1949 年到 1960 年期間的每個月航班乘客的數(shù)量糖耸。顏色越淺表示乘客越多。
import matplotlib.pyplot as plt
import seaborn as sn
flights = sn.load_dataset("flights")
data = flights.pivot('year', 'month', 'passengers')
sn.heatmap(data)
plt.show()
餅圖:餅圖常用于統(tǒng)計學模塊丘薛,用于顯示各塊的比例嘉竟。
import matplotlib.pyplot as plt
nums = [12, 45, 29, 46, 30]
labels = ['A', 'B', 'C', 'D', 'E']
plt.pie(x=nums, labels=labels)
plt.show()
現(xiàn)在對比一下Matplotlib和Seaborn這兩個庫畫圖的不同之處
首先導(dǎo)入我們需要用到的庫:
import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np
import pandas as pd
1.直方圖:一種二維統(tǒng)計圖表,用圖形表示數(shù)據(jù)的分布情況
直方圖matplotlib:
x = np.random.randn(100)
y = pd.Series(x)
plt.hist(y)
plt.show()
直方圖seaborn :
x = np.random.randn(200)
y = pd.Series(x)
# sn.distplot(y, kde=False)#Seaborn 畫直方圖洋侨,當參數(shù) kde=False 時舍扰,和 上面 Matplotlib 畫的直方圖基本一樣
sn.distplot(y)#當 kde 參數(shù)默認不填或者設(shè)置當參數(shù) kde=True 時,在上圖基礎(chǔ)上希坚,會顯示核密度估計边苹,這可以幫助我們估計概率密度。
plt.show()
2.條形圖:條形圖是用寬度相同的條形的高度或長短來表示數(shù)據(jù)多少的圖形裁僧。
條形圖matplotlib:
x = ["a", "b", "c", "d", "e", "f"]
y = [30, 47, 88, 25, 93, 101]
plt.bar(x, y)
plt.show()
條形圖seaborn :
x = ["a", "b", "c", "d", "e", "f"]
y = [30, 47, 88, 25, 93, 101]
sn.barplot(x, y)
plt.show()
通過生成的效果圖可以看出个束,Seaborn 生成的條形圖默認顏色不一樣慕购,視覺效果更好些。
3.折線圖:可以顯示隨時間而變化的連續(xù)數(shù)據(jù)茬底,因此非常適用于顯示在相等時間間隔下數(shù)據(jù)的趨勢沪悲。
折線圖matplotlib:
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [50, 25, 70, 200, 170, 160, 190, 300, 320, 350]
plt.plot(x, y)
plt.show()
折線圖seaborn:
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [50, 25, 70, 200, 170, 160, 190, 300, 320, 350]
df = pd.DataFrame({'x': x, 'y': y})
sn.lineplot(x="x", y="y", data=df)
plt.show()
這兩個庫的效果幾乎一樣,只是 seaborn 庫畫的圖標注了 x 和 y 坐標的位置阱表。
4.散點圖:散點圖是指在回歸分析中殿如,數(shù)據(jù)點在直角坐標系平面上的分布圖,散點圖表示因變量隨自變量而變化的大致趨勢最爬,據(jù)此可以選擇合適的函數(shù)對數(shù)據(jù)點進行擬合涉馁。
散點圖matplotlib:
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
plt.scatter(x, y, marker='x')
plt.show()
散點圖seaborn:
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
df = pd.DataFrame({'x': x, 'y': y})
sn.jointplot(x="x", y="y", data=df, kind='scatter')
plt.show()
matplotlib庫畫的圖默認是長方形,seaborn 庫畫圖默認是正方形爱致,不僅畫出了散點圖烤送,還給出了他們的分布情況.
5.箱型圖:是一種用作顯示一組數(shù)據(jù)分散情況資料的統(tǒng)計圖
生成 0-1 之間的 10*5 維度數(shù)據(jù)
箱型圖matplotlib:
data=np.random.normal(size=(10, 5))
lables = ["a", "b", "c", "d", "e"]
plt.boxplot(data, labels=lables)
plt.show()
箱型圖seaborn:
data=np.random.normal(size=(10, 5))
lables = ["a", "b", "c", "d", "e"]
df = pd.DataFrame(data, columns=lables)
sn.boxplot(data=df)
plt.show()
Seaborn 生成的箱型圖也是彩色的,視覺效果好蒜鸡。