溫馨提示×

Seaborn怎么按類別計數(shù)并可視化

小億
84
2024-05-17 11:14:14
欄目: 編程語言

您可以使用Seaborn中的countplot()函數(shù)來按類別計數(shù)并可視化數(shù)據(jù)。下面是一個示例代碼,展示如何使用Seaborn按類別計數(shù)并可視化數(shù)據(jù):

import seaborn as sns
import matplotlib.pyplot as plt

# 創(chuàng)建示例數(shù)據(jù)
data = {'Category': ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C'],
        'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}

# 將數(shù)據(jù)轉(zhuǎn)換為DataFrame
df = pd.DataFrame(data)

# 使用countplot函數(shù)按類別計數(shù)并可視化數(shù)據(jù)
sns.countplot(x='Category', data=df)

# 添加標題和標簽
plt.title('Count of Values by Category')
plt.xlabel('Category')
plt.ylabel('Count')

# 顯示圖形
plt.show()

運行上面的代碼將生成一個按類別計數(shù)的條形圖,顯示每個類別中值的數(shù)量。您可以根據(jù)自己的數(shù)據(jù)和需求調(diào)整代碼以適應(yīng)您的情況。

0