Python Pandas - 使用Seaborn繪製小提琴圖,指定順序並顯示觀測值作為線條
Seaborn中的小提琴圖用於繪製箱線圖和核密度估計的組合。 seaborn.violinplot() 用於此目的。 使用**inner**引數,其值為**stick**,可以將觀測值顯示為線條。
假設我們的資料集是一個CSV檔案,如下所示:Cricketers.csv
首先,匯入所需的庫:
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt
將CSV檔案中的資料載入到Pandas DataFrame中:
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv")
繪製帶有學院和年齡的小提琴圖。透過傳遞顯式順序(即基於“學院”的排序)來控制順序。使用inner引數,值為stick,將觀測值顯示為線條:
sb.violinplot(x = 'Academy', y = "Age", order=["Western Australia", "Tasmania","South Australia"],data = dataFrame, inner="stick")
示例
程式碼如下:
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Load data from a CSV file into a Pandas DataFrame:
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv")
# plotting violin plot with Academy and Age
# Control order by passing an explicit order i.e. ordering on the basis of "Academy"
# observations show as a stick using the inner parameter with value stick
sb.violinplot(x = 'Academy', y = "Age", order=["Western Australia", "Tasmania","South Australia"],data = dataFrame, inner="stick")
# display
plt.show()輸出
這將產生以下輸出:

廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP