Python Pandas - 繪製條形圖並使用中位數作為集中趨勢的估計值
Seaborn中的條形圖用於顯示點估計和置信區間作為矩形條。為此使用seaborn.barplot()。繪製具有資料集列作為x和y值的水平條形圖。使用**estimator**引數將**中位數**設定為集中趨勢的估計值。
假設以下資料集以CSV檔案的形式給出:Cricketers2.csv
首先,匯入所需的庫:
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt from numpy import median
將資料從CSV檔案載入到Pandas DataFrame中:
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv")
使用estimator引數將中位數設定為集中趨勢的估計值,繪製使用比賽和學院的水平條形圖:
sb.barplot(x = dataFrame["Academy"], y = dataFrame["Matches"], estimator = median)
示例
以下是程式碼:
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt from numpy import median # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") # plotting horizontal bar plots with Matches and Academy # using the estimator parameter to set median as the estimate of central tendency sb.barplot(x = dataFrame["Academy"], y = dataFrame["Matches"], estimator = median) # display plt.show()
輸出
這將產生以下輸出:
廣告