Python 數(shù)據(jù)可視化之Seaborn詳解
安裝
要安裝 seaborn,請(qǐng)?jiān)诮K端中輸入以下命令。
pip install seaborn
Seaborn 建立在 Matplotlib 之上,因此它也可以與 Matplotlib 一起使用。一起使用 Matplotlib 和 Seaborn 是一個(gè)非常簡(jiǎn)單的過(guò)程。我們只需要像之前一樣調(diào)用 Seaborn Plotting 函數(shù),然后就可以使用 Matplotlib 的自定義函數(shù)了。
注意: Seaborn 加載了提示、虹膜等數(shù)據(jù)集,但在本教程中,我們將使用 Pandas 加載這些數(shù)據(jù)集。
例子:
# 導(dǎo)包 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 讀取數(shù)據(jù)庫(kù) data = pd.read_csv("tips.csv") # 畫(huà)線圖 sns.lineplot(x="sex", y="total_bill", data=data) # 使用 Matplotlib 設(shè)置標(biāo)題 plt.title('Title using Matplotlib Function') plt.show()
輸出:
散點(diǎn)圖
散點(diǎn)圖是使用scatterplot() 方法繪制的。這類似于 Matplotlib,但需要額外的參數(shù)數(shù)據(jù)。
# 導(dǎo)包 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 讀取數(shù)據(jù)庫(kù) data = pd.read_csv("tips.csv") sns.scatterplot(x='day', y='tip', data=data,) plt.show()
輸出:
你會(huì)發(fā)現(xiàn)在使用 Matplotlib 時(shí),如果你想根據(jù)sex為這個(gè)圖的每個(gè)點(diǎn)著色會(huì)很困難。 但在散點(diǎn)圖中,它可以在色調(diào)參數(shù)的幫助下完成。
例子:
# 導(dǎo)包 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 讀取數(shù)據(jù)庫(kù) data = pd.read_csv("tips.csv") sns.scatterplot(x='day', y='tip', data=data, hue='sex') plt.show()
輸出:
線圖
Seaborn 中的 Line Plot 使用 lineplot()
方法繪制。 在這種情況下,我們也可以只傳遞 data 參數(shù)。
示例:
# 導(dǎo)包 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 讀取數(shù)據(jù)庫(kù) data = pd.read_csv("tips.csv") sns.lineplot(x='day', y='tip', data=data) plt.show()
輸出:
示例 2:
# 導(dǎo)包 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 讀取數(shù)據(jù)庫(kù) data = pd.read_csv("tips.csv") # 僅使用數(shù)據(jù)屬性 sns.lineplot(data=data.drop(['total_bill'], axis=1)) plt.show()
輸出:
條形圖
Seaborn 中的條形圖可以使用barplot()
方法.
例子:
# 導(dǎo)包 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 讀取數(shù)據(jù)庫(kù) data = pd.read_csv("tips.csv") sns.barplot(x='day',y='tip', data=data, hue='sex') plt.show()
輸出:
直方圖
Seaborn 中的直方圖可以使用histplot()
函數(shù)繪制。
例子:
# 導(dǎo)包 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 讀取數(shù)據(jù)庫(kù) data = pd.read_csv("tips.csv") sns.histplot(x='total_bill', data=data, kde=True, hue='sex') plt.show()
輸出:
在瀏覽完所有這些繪圖后,您一定已經(jīng)注意到,使用 Seaborn 自定義繪圖比使用 Matplotlib 容易得多。 它也是基于 matplotlib 構(gòu)建的,那么我們也可以在使用 Seaborn 時(shí)使用 matplotlib 函數(shù)。下一節(jié)我們繼續(xù)談第三個(gè)庫(kù)——Bokeh
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。