我無法找到將根據統計資料創建的多個箱線圖繪制成一個圖形的解決方案。
從另一個應用程式中,我得到一個 Dataframe,其中包含繪制箱線圖所需的不同指標(中位數、分位數 1,...)。雖然我可以使用以下代碼從這些統計資料中繪制單個箱線圖:
data = pd.read_excel("data.xlsx")
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), sharey=True)
row = data.iloc[:, 0]
stats = [{
"label": i, # not required
"mean": row["sharpeRatio"], # not required
"med": row["sharpeRatio_med"],
"q1": row["sharpeRatio_q1"],
"q3": row["sharpeRatio_q3"],
# "cilo": 5.3 # not required
# "cihi": 5.7 # not required
"whislo": row["sharpeRatio_min"], # required
"whishi": row["sharpeRatio_max"], # required
"fliers": [] # required if showfliers=True
}]
axes.bxp(stats)
plt.show()
我正在努力創建一個包含來自資料框中所有行的箱線圖的圖形。您知道如何實作這一目標嗎?
uj5u.com熱心網友回復:
您可以將字典串列傳遞給該bxp方法。從現有代碼中獲取此類串列的最簡單方法是將字典構造放在函式中,并為資料幀的每一行呼叫它。
請注意,這data.iloc[:, 0]將是第一列,而不是第一行。
import matplotlib.pyplot as plt
import pandas as pd
def stats(row):
return {"med": row["sharpeRatio_med"],
"q1": row["sharpeRatio_q1"],
"q3": row["sharpeRatio_q3"],
"whislo": row["sharpeRatio_min"],
"whishi": row["sharpeRatio_max"]}
data = pd.DataFrame({"sharpeRatio_med": [3, 4, 2],
"sharpeRatio_q1": [2, 3, 1],
"sharpeRatio_q3": [4, 5, 3],
"sharpeRatio_min": [1, 1, 0],
"sharpeRatio_max": [5, 6, 4]})
fig, axes = plt.subplots()
axes.bxp([stats(data.iloc[i, :]) for i in range(len(data))],
showfliers=False)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/342637.html
標籤:Python matplotlib
下一篇:如何在圖表上設定xticks
