散點(diǎn)圖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 數(shù)據(jù)準(zhǔn)備
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
plt.scatter(x, y,marker='x')??# 用 Matplotlib 畫散點(diǎn)圖
plt.show()
df = pd.DataFrame({'x': x, 'y': y})??# 用 Seaborn 畫散點(diǎn)圖,會(huì)顯示數(shù)據(jù)的分布情況
sns.jointplot(x="x", y="y", data=df, kind='scatter');
plt.show()
折線圖
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [5, 3, 6, 20, 17, 16, 19, 30, 32, 35]
# 使用 Matplotlib 畫折線圖
plt.plot(x, y)
plt.show()
# 使用 Seaborn 畫折線圖
df = pd.DataFrame({'x': x, 'y': y})
sns.lineplot(x="x", y="y", data=df)
plt.show()
直方圖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 數(shù)據(jù)準(zhǔn)備
a = np.random.randn(100)
s = pd.Series(a)
# 用 Matplotlib 畫直方圖,?plt.hist(x, bins=10) 函數(shù),其中參數(shù) x 是一維數(shù)組,bins 代表直方圖中的箱子數(shù)量,默認(rèn)是 10。
plt.hist(s)
plt.show()
# 用 Seaborn 畫直方圖,使用 sns.distplot(x, bins=10, kde=True) 函數(shù)。其中參數(shù) x 是一維數(shù)組,bins 代表直方圖中的箱子數(shù)量,kde 代表顯示核密度估計(jì),默認(rèn)是 True,我們也可以把 kde 設(shè)置為 False,不進(jìn)行顯示。核密度估計(jì)是通過(guò)核函數(shù)幫我們來(lái)估計(jì)概率密度的方法。
sns.distplot(s, kde=False)
plt.show()
sns.distplot(s, kde=True)
plt.show()
條形圖
import matplotlib.pyplot as plt
import seaborn as sns
# 數(shù)據(jù)準(zhǔn)備
x = ['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5']
y = [5, 4, 8, 12, 7]
# 用 Matplotlib 畫條形圖
plt.bar(x, y)
plt.show()
# 用 Seaborn 畫條形圖
sns.barplot(x, y)
plt.show()
箱線圖(
盒式圖
)
由五個(gè)數(shù)值點(diǎn)組成:最大值 (max)、最小值 (min)、中位數(shù) (median) 和上下四分位數(shù) (Q3, Q1)。它可以幫我們分析出數(shù)據(jù)的差異性、離散程度和異常值等。
# 數(shù)據(jù)準(zhǔn)備
# 生成 0-1 之間的 10*4 維度數(shù)據(jù)
data=np.random.normal(size=(10,4))
lables = ['A','B','C','D']
# 用 Matplotlib 畫箱線圖,使用 plt.boxplot(x, labels=None) 函數(shù),其中參數(shù) x 代表要繪制箱線圖的數(shù)據(jù),labels 是缺省值,可以為箱線圖添加標(biāo)簽。
plt.boxplot(data,labels=lables)
plt.show()
# 用 Seaborn 畫箱線圖,使用 sns.boxplot(x=None, y=None, data=None) 函數(shù)。其中參數(shù) data 為 DataFrame 類型,x、y 是 data 中的變量。
df = pd.DataFrame(data, columns=lables)
sns.boxplot(data=df)
plt.show()
餅圖
import matplotlib.pyplot as pl
# 數(shù)據(jù)準(zhǔn)備
nums = [25, 37, 33, 37, 6]
labels = ['High-school','Bachelor','Master','Ph.d', 'Others']
# 用 Matplotlib 畫餅圖,們使用 plt.pie(x, labels=None) 函數(shù),其中參數(shù) x 代表要繪制餅圖的數(shù)據(jù),labels 是缺省值,可以為餅圖添加標(biāo)簽。
plt.pie(x = nums, labels=labels)
plt.show()
熱力圖
import matplotlib.pyplot as plt
import seaborn as sns
# 數(shù)據(jù)準(zhǔn)備
flights = sns.load_dataset("flights")
data=flights.pivot('year','month','passengers')
# 用 Seaborn 畫熱力圖
sns.heatmap(data)
plt.show()
蜘蛛圖
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.font_manager import FontProperties
labels=np.array([u" 推進(jìn) ","KDA",u" 生存 ",u" 團(tuán)戰(zhàn) ",u" 發(fā)育 ",u" 輸出 "])
stats=[83, 61, 95, 67, 76, 88]
# 畫圖數(shù)據(jù)準(zhǔn)備,角度、狀態(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]]))
# 用 Matplotlib 畫蜘蛛圖
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()
二元變量分布?
import matplotlib.pyplot as pl
import seaborn as sns
tips = sns.load_dataset("tips")
print(tips.head(10))
# 用 Seaborn 畫二元變量分布圖(散點(diǎn)圖,核密度圖,Hexbin 圖) 使用 sns.jointplot(x, y, data=None, kind) 函數(shù)即可。其中用 kind 表示不同的視圖類型:“kind=‘scatter’”代表散點(diǎn)圖,“kind=‘kde’”代表核密度圖,“kind=‘hex’ ”代表 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')
plt.show()
成對(duì)關(guān)系
import matplotlib.pyplot as pl
import seaborn as sns
iris = sns.load_dataset('iris')? #鳶尾花數(shù)據(jù)集。鳶尾花可以分成 Setosa、Versicolour 和 Virginica 三個(gè)品種,在這個(gè)數(shù)據(jù)集中,針對(duì)每一個(gè)品種,都有 50 個(gè)數(shù)據(jù),每個(gè)數(shù)據(jù)中包括了 4 個(gè)屬性,分別是花萼長(zhǎng)度、花萼寬度、花瓣長(zhǎng)度和花瓣寬度。通過(guò)這些數(shù)據(jù),需要你來(lái)預(yù)測(cè)鳶尾花卉屬于三個(gè)品種中的哪一種。
# 用 Seaborn 畫成對(duì)關(guān)系
sns.pairplot(iris)
plt.show()
