Home > Software design >  Multiple boxplots on the same graph
Multiple boxplots on the same graph

Time:02-02

I need to create multiple boxplots on the same graph. The sports are 3. I need to obtain 3 boxplots on the same graph of each sport, with a specific variable on the y-axis. I need to be able to change the variable. The variable for each student is registered various times and is given by the mean of the 3 largest numbers. I have 30 students identified with an ID (that goes from 1 to 30). Each student does only one sport. This is what I wrote but clearly it doesn't work. Can someone help? I hope my explanation made sense.

def boxplot(sport, variable):
    list=[]
    for l in range(1,31):
        g = df[(df.ID == l) & (df.sport == sport)][variable].nlargest(n=3).mean()
        list.append(g)
    my_dict = {f'Boxplot for {variable} in {sport}': list}
    fig, ax = plt.subplots()
    ax.boxplot(my_dict.values())
    ax.set_xticklabels(my_dict.keys())
    plt.show()

CodePudding user response:

Here's one way to do it.

import plotly.express as px

df = px.data.tips()

fig = px.box(df, x="day", y="total_bill", color="smoker")
fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
fig.show()

enter image description here

If you data is not melted or stacked, you can change the layout like this.

enter image description here

Here is one final update. Make sure the y-axis is numeric...

import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plot

df = px.data.tips()
df=pd.DataFrame(df)
print(type(df))
df.head()
df.columns = ['total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size']

b_plot = df.boxplot(column = ['tip','size','total_bill']) 
b_plot.plot()
plot.show()

enter image description here

  • Related