简体   繁体   中英

Create multiple boxplots from statistics in one graph

I am having trouble finding a solution to plot multiple boxplots created from statistics into one graph.

From another application, I get a Dataframe that contains the different metrics needed to draw boxplots (median, quantile 1, ...). While I am able to plot a single boxplot from these statistics with the following code:

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()

I am struggling to create a graph containing boxplots from all the rows in the dataframe. Do you have an idea how to achieve this?

You can pass a list of dictionaries to the bxp method. The easiest way to get such a list from your existing code is to put the dictionary construction inside a function and call it for each row of the dataframe.

Note that data.iloc[:, 0] would be the first column, not the first row.

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()

三个箱线图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM