python图形界面实践_数据可视化之实践篇——python
一.10個可視化例子
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
1.散點圖
plt: plt.scatter(x, y, marker=None) 函數(shù)。x、y 是坐標,marker 代表了標記的符號。比如“x”、“>”或者“o”。選擇不同的 marker,呈現(xiàn)出來的符號樣式也會不同。
sns: sns.jointplot(x, y, data=None, kind=‘scatter’) 函數(shù)。其中 x、y 是 data 中的下標。data 就是我們要傳入的數(shù)據(jù),一般是 DataFrame 類型。kind 這類我們?nèi)?scatter,代表散點的意思。當然 kind 還可以取其他值,這個我在后面的視圖中會講到,不同的 kind 代表不同的視圖繪制方式。
matplotlib繪制的視圖為矩形,seaborn為方形,且還額外顯示x,y的直方圖分布
# 隨機1000個點,模擬繪制
n = 1000
x = np.random.randn(n)
y = np.random.randn(n)
# use matplotlib to draw
plt.scatter(x, y , marker='x')
plt.show()
plt.scatter(x, y , marker='>')
plt.show()
plt.scatter(x, y , marker='o')
plt.show()
#?plt.scatter
# use seaborn to draw
df = pd.DataFrame({'x':x, 'y':y})
sns.jointplot(x='x', y='y', data=df, kind='scatter')
plt.show()
2.折線圖
表示數(shù)據(jù)隨時間變化趨勢
在 Matplotlib 中,我們可以直接使用 plt.plot() 函數(shù),當然需要提前把數(shù)據(jù)按照 x 軸的大小進行排序,要不畫出來的折線圖就無法按照 x 軸遞增的順序展示。
在 Seaborn 中,我們使用 sns.lineplot (x, y, data=None) 函數(shù)。其中 x、y 是 data 中的下標。data 就是我們要傳入的數(shù)據(jù),一般是 DataFrame 類型。
這里我們設置了 x、y 的數(shù)組。x 數(shù)組代表時間(年),y 數(shù)組我們隨便設置幾個取值
兩個庫繪制出來結(jié)果一致。
# data
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [5, 3, 6, 20, 17, 16, 19, 30, 32, 35]
# use matplotlib
plt.plot(x, y)
plt.show()
# use seaborn
df = pd.DataFrame({'x':x, 'y':y})
sns.lineplot(x='x', y='y', data=df)
plt.show()
3. 直方圖
直方圖用以查看變量的數(shù)值分布。
在 Matplotlib 中,我們使用 plt.hist(x, bins=10) 函數(shù),其中參數(shù) x 是一維數(shù)組,bins 代表直方圖中的箱子數(shù)量,默認是 10。
在 Seaborn 中,我們使用 sns.distplot(x, bins=10, kde=True) 函數(shù)。其中參數(shù) x 是一維數(shù)組,bins 代表直方圖中的箱子數(shù)量,kde 代表顯示核密度估計,默認是 True,我們也可以把 kde 設置為 False,不進行顯示。
核密度估計是通過核函數(shù)幫我們來估計概率密度的方法。
# data
a = np.random.randn(100)
s = pd.Series(a)
# use matplotlib
plt.hist(s)
plt.show()
# use seaborn
sns.distplot(s, kde=False)
plt.show()
sns.distplot(s, kde=True)
plt.show()
4.條形圖
條形圖查看類別特征。
在條形圖中,長條形的長度表示類別的頻數(shù),寬度表示類別。
在 Matplotlib 中,我們使用 plt.bar(x, height) 函數(shù),其中參數(shù) x 代表 x 軸的位置序列,height 是 y 軸的數(shù)值序列,也就是柱子的高度。
在 Seaborn 中,我們使用 sns.barplot(x=None, y=None, data=None) 函數(shù)。其中參數(shù) data 為 DataFrame 類型,x、y 是 data 中的變量。
# data
x = ['cat1', 'cat2', 'cat3', 'cat4', 'cat5'] # 類別
y = [5, 4, 8, 12, 7] # 頻數(shù)
# use matplotlib
plt.bar(x, y)
plt.show()
# use seaborn
sns.barplot(x, y)
plt.show()
5. 箱線圖
箱線圖,又稱盒式圖,它是在 1977 年提出的,由五個數(shù)值點組成:最大值 (max)、最小值 (min)、中位數(shù) (median) 和上下四分位數(shù) (Q3, Q1)。
它可以幫我們分析出數(shù)據(jù)的差異性、離散程度和異常值等。
在 Matplotlib 中,我們使用 plt.boxplot(x, labels=None) 函數(shù),其中參數(shù) x 代表要繪制箱線圖的數(shù)據(jù),labels 是缺省值,可以為箱線圖添加標簽。
在 Seaborn 中,我們使用 sns.boxplot(x=None, y=None, data=None) 函數(shù)。其中參數(shù) data 為 DataFrame 類型,x、y 是 data 中的變量。
# data:10*4維度
data = np.random.normal(size=(10, 4)) # [0,1]10*4維數(shù)據(jù)
lables = ['A', 'B', 'C', 'D']
data
array([[-2.23835375, 0.86815454, -0.03632889, 0.7446558 ],
[-1.10741816, -0.78987338, 0.74452918, -0.64852577],
[ 1.38908051, -1.00353393, -1.92624668, 0.2354071 ],
[ 1.88915876, 1.26500499, 0.32391999, -0.06991769],
[-0.76832767, -0.9832141 , -0.02364687, 0.20468148],
[-1.11151298, 0.70348759, 1.57796211, 0.30206291],
[-0.11900145, 0.35820823, -1.37098595, 1.64935728],
[-0.74382552, 0.32722696, -0.59096453, 0.8708664 ],
[-1.21016063, 0.97042535, 1.31342357, -0.68687588],
[ 0.23279038, -0.4774945 , 0.3546045 , 0.82579075]])
# use matplotlib
plt.boxplot(data, labels=lables)
plt.show()
# use seaborn
df = pd.DataFrame(data, columns=lables)
sns.boxplot(data=df)
plt.show()
6.餅圖
餅圖是常用的統(tǒng)計學模塊,可以顯示每個部分大小與總和之間的比例。
在 Python 數(shù)據(jù)可視化中,它用的不算多。
我們主要采用 Matplotlib 的 pie 函數(shù)實現(xiàn)它。在 Matplotlib 中,我們使用 plt.pie(x, labels=None) 函數(shù),其中參數(shù) x 代表要繪制餅圖的數(shù)據(jù),labels 是缺省值,可以為餅圖添加標簽。
# data
nums = [25, 37, 33, 37, 6]
labels = ['high-school', 'bachelor', 'master', 'ph.d', 'others']
# matplotlib
plt.pie(x=nums, labels=labels)
plt.show()
7.熱力圖
熱力圖,英文叫 heat map,是一種矩陣表示方法,其中矩陣中的元素值用顏色來代表,不同的顏色代表不同大小的值。
通過顏色就能直觀地知道某個位置上數(shù)值的大小。另外你也可以將這個位置上的顏色,與數(shù)據(jù)集中的其他位置顏色進行比較。
熱力圖是一種非常直觀的多元變量分析方法。
我們一般使用 Seaborn 中的 sns.heatmap(data) 函數(shù),其中 data 代表需要繪制的熱力圖數(shù)據(jù)。
# data
# 使用 Seaborn 中自帶的數(shù)據(jù)集 flights,該數(shù)據(jù)集記錄了 1949 年到 1960 年期間,每個月的航班乘客的數(shù)量。
flights = pd.read_excel('flights.xlsx')
data = flights.pivot('year', 'month', 'passengers')
data.head()monthAprilAugustDecemberFebruaryJanuaryJulyJuneMarchMayNovemberOctoberSeptemberyear
# use seaborn
sns.heatmap(data)
plt.show()
# 顏色越淺乘客越多
8.蜘蛛圖/雷達圖
蜘蛛圖是一種顯示一對多關系的方法。在蜘蛛圖中,一個變量相對于另一個變量的顯著性是清晰可見的。
假設我們想要給王者榮耀的玩家做一個戰(zhàn)力圖,指標一共包括推進、KDA、生存、團戰(zhàn)、發(fā)育和輸出。那該如何做呢?
這里我們需要使用 Matplotlib 來進行畫圖,首先設置兩個數(shù)組:labels 和 stats。
他們分別保存了這些屬性的名稱和屬性值。
因為蜘蛛圖是一個圓形,你需要計算每個坐標的角度,然后對這些數(shù)值進行設置。
當畫完最后一個點后,需要與第一個點進行連線。
因為需要計算角度,所以我們要準備 angles 數(shù)組;又因為需要設定統(tǒng)計結(jié)果的數(shù)值,所以我們要設定 stats 數(shù)組。并且需要在原有 angles 和 stats 數(shù)組上增加一位,也就是添加數(shù)組的第一個元素。
from matplotlib.font_manager import FontProperties
# data
labels = np.array(['推進','kda', '生存', '團戰(zhàn)', '發(fā)育', '輸出'])
stats = [83, 61, 95, 67, 76, 88]
# 角度,狀態(tài)值
angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats = np.concatenate((stats, [stats[0]]))
angles = np.concatenate((angles, [angles[0]]))
# use matplotlib
fig = plt.figure() # 準備一個空白畫板
ax = fig.add_subplot(111, polar=True) # 畫板分成1行1列
ax.plot(angles, stats, alpha=0.25) # 連線
ax.fill(angles, stats, alpha=0.25) # 上色
#設置中文字體
#font = FontProperties(fname=r'', size=14)
ax.set_thetagrids(angles * 180 / np.pi, labels) # 顯示屬性名
plt.show()
9.二元變量分布
如果我們想要看兩個變量之間的關系,就需要用到二元變量分布。
當然二元變量分布有多種呈現(xiàn)方式,開頭給你介紹的散點圖就是一種二元變量分布。
在 Seaborn 里,使用二元變量分布是非常方便的,直接使用 sns.jointplot(x, y, data=None, kind) 函數(shù)即可。
其中用 kind 表示不同的視圖類型:“kind=‘scatter’”代表散點圖,“kind=‘kde’”代表核密度圖,“kind=‘hex’ ”代表 Hexbin 圖,它代表的是直方圖的二維模擬。
這里我們使用 Seaborn 中自帶的數(shù)據(jù)集 tips,這個數(shù)據(jù)集記錄了不同顧客在餐廳的消費賬單及小費情況。
代碼中 total_bill 保存了客戶的賬單金額,tip 是該客戶給出的小費金額。
我們可以用 Seaborn 中的 jointplot 來探索這兩個變量之間的關系。
# data
tips = pd.read_excel('tips.xlsx')
tips.head()total_billtipsexsmokerdaytimesize
# 用Seaborn畫二元變量分布圖(散點圖,核密度圖,Hexbin圖)
sns.jointplot(x='total_bill', y='tip', data=tips, kind='scatter') # 散點圖
sns.jointplot(x='total_bill', y='tip', data=tips, kind='kde') # 核密度圖
sns.jointplot(x='total_bill', y='tip', data=tips, kind='hex') # hexbin圖
plt.show()
10.成對關系
如果想要探索數(shù)據(jù)集中的多個成對雙變量的分布,可以直接采用 sns.pairplot() 函數(shù)。
它會同時展示出 DataFrame 中每對變量的關系,另外在對角線上,你能看到每個變量自身作為單變量的分布情況。
它可以說是探索性分析中的常用函數(shù),可以很快幫我們理解變量對之間的關系。
pairplot 函數(shù)的使用,就像在 DataFrame 中使用 describe() 函數(shù)一樣方便,是數(shù)據(jù)探索中的常用函數(shù)。
# data
#使用 Seaborn 中自帶的 iris 數(shù)據(jù)集,這個數(shù)據(jù)集也叫鳶尾花數(shù)據(jù)集。
#鳶尾花可以分成 Setosa、Versicolour 和 Virginica 三個品種,
#在這個數(shù)據(jù)集中,針對每一個品種,都有 50 個數(shù)據(jù),每個數(shù)據(jù)中包括了 4 個屬性,
#分別是花萼長度、花萼寬度、花瓣長度和花瓣寬度。
#通過這些數(shù)據(jù),需要你來預測鳶尾花卉屬于三個品種中的哪一種。
iris = pd.read_csv('seaborn-data-master/iris.csv')
iris.head()sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
# use seaborn
sns.pairplot(iris)
plt.show()
# 顯示了4個變量兩兩之間的關系:
# sepal_length、sepal_width、petal_length 和 petal_width4,對應:
# 花萼長度、花萼寬度、花瓣長度和花瓣寬度
二.可視化庫一覽
1.seaborn
Seaborn是基于matplotlib的Python數(shù)據(jù)可視化庫。它提供了一個高級界面,用于繪制引人入勝且內(nèi)容豐富的統(tǒng)計圖形。
類似于:pandas比之numpy。
案例:
以下是相關繪制的api。
2.matplot
3.scipy
4.plotly
總結(jié)
以上是生活随笔為你收集整理的python图形界面实践_数据可视化之实践篇——python的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在电脑上下载并安装快手直播伴侣
- 下一篇: 侠客风云传傅剑寒攻略