Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple boxplots in seaborn?

I want to plot boxplots using seaborn in pandas because it is a nicer way to visualize data, but I am not too familiar with it. I have three dataframes that are different metrics, and I want to compare the different metrics. I will loop through the file paths to access them.

for path in paths: 
   df = pd.read_csv(path)

The dfs for each of the metrics are separate and look something like this (where the .... indicates filled in data values). 1, 2, 3, 4, 5 are the column names and indicate different trials :

    1  2  3  4  5
0   ..............
1   ..............
2   ..............
3   ..............
4   ..............

I want to have all the plots for trials 1, 2, 3, 4, 5 and each of the 3 metrics side by side, where all the first trial plots for the three metrics would be on the left, then all the second trial plots would be to the right of that, and so on.

How could I go about doing this in seaborn? I know I can do a plot individually for each metric by looping through the path and using the boxplot function like this:

sns.boxplot(data=df)   

However, how would I be able to fit the other metrics' plots side-by-side on the same plot?

like image 790
Jane Sully Avatar asked Jun 14 '17 18:06

Jane Sully


People also ask

How do you draw multiple Boxplots in Seaborn?

To draw a box plot with Seaborn, the boxplot() function is used. You can either pass the full dataframe column names to the x or y parameters or you can simply specify the column names in the x and y parameters and then specify the dataframe name in the dataset parameter.

How do you plot multiple plots in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.

How do you draw multiple Boxplots?

In this article, we will learn how to plot multiple boxplot in one graph in R Programming Language. This can be accomplished by using boxplot() function, and we can also pass in a list, data frame or multiple vectors to it. For this purpose, we need to put name of data into boxplot() function as input.


1 Answers

Consider first assigning a grouping column like Trial for each corresponding dataframe, then pd.concat your dataframes, and finally pd.melt the data for a indicator/value long-wise dataframe before plotting with seaborn. Below demonstrates with random data:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns

np.random.seed(44)
# DATAFRAMES WITH TRIAL COLUMN ASSIGNED
df1 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=1)
df2 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=2)
df3 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=3)

cdf = pd.concat([df1, df2, df3])                                # CONCATENATE
mdf = pd.melt(cdf, id_vars=['Trial'], var_name=['Number'])      # MELT

print(mdf.head())
#    Trial Number     value
# 0      1      1 -0.750615
# 1      1      1 -1.715070
# 2      1      1 -0.963404
# 3      1      1  0.360856
# 4      1      1 -1.190504

ax = sns.boxplot(x="Trial", y="value", hue="Number", data=mdf)  # RUN PLOT   
plt.show()

plt.clf()
plt.close()

enter image description here

like image 70
Parfait Avatar answered Oct 06 '22 01:10

Parfait