confused_intern
confused_intern

Reputation: 59

How to combine two boxplots with the same axes into one boxplot in Python

How do I combine two independent box plots with the same axes into one box plot? The data all originate from the same data frame.

I have two graphs that I am trying to combine into one:

Graph 1)

enter image description here

Graph 2)

enter image description here

How do I combine them such that they look something like this (similar to when the hue parameter is used):

enter image description here

My current data frame looks like this. Note that I manually added a 'Data Type' column just so I can use the hue parameter in sns.boxplot to showcase my example. The 'Data Type' column is NOT in the actual data frame:

         Annualized Return  Annualized Volatility Weighting Method   Data Type
0             0.100279               0.018287    Equal Weights     Returns
1             0.052186               0.019462    Equal Weights  Volatility
2             0.066412               0.021039    Equal Weights     Returns
3             0.037828               0.030207    Equal Weights  Volatility
4             0.083212               0.016781    Equal Weights     Returns
..                 ...                    ...              ...         ...
195           0.064490               0.019199              ERC  Volatility
196           0.074595               0.015279              ERC     Returns
197           0.048052               0.015284              ERC  Volatility
198           0.053672               0.013398              ERC     Returns
199           0.054881               0.018141              ERC  Volatility

This is the code I used to produce the desired-looking output. Again, the hue parameter is manually added just for visualization purposes:

sns.boxplot(x='Weighting Method',y = 'Annualized Volatility',data=df,showfliers=False,color='tomato',hue='Data Type')

Upvotes: 1

Views: 7298

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62393

  • The dataframe needs to be converted from a wide format to a long format
  • Use pandas.melt
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = {'Annualized Return': [0.100279, 0.052186, 0.066412, 0.037828, 0.083212, 0.06448999999999999, 0.07459500000000001, 0.048052, 0.053672000000000004, 0.05488099999999999],
        'Annualized Volatility': [0.018287, 0.019462, 0.021039, 0.030206999999999998, 0.016781, 0.019199, 0.015279, 0.015284, 0.013397999999999998, 0.018141],
        'Weighting Method': ['Equal Weights', 'Equal Weights', 'Equal Weights', 'Equal Weights', 'Equal Weights', 'ERC', 'ERC', 'ERC', 'ERC', 'ERC']}

df = pd.DataFrame(data)

# display df.head()
   Annualized Return  Annualized Volatility Weighting Method
0           0.100279               0.018287    Equal Weights
1           0.052186               0.019462    Equal Weights
2           0.066412               0.021039    Equal Weights
3           0.037828               0.030207    Equal Weights
4           0.083212               0.016781    Equal Weights

# convert dataframe from wide to long
dfl = pd.melt(df, id_vars='Weighting Method', value_vars=['Annualized Return', 'Annualized Volatility'])

# display dfl.head()
  Weighting Method           variable     value
0    Equal Weights  Annualized Return  0.100279
1    Equal Weights  Annualized Return  0.052186
2    Equal Weights  Annualized Return  0.066412
3    Equal Weights  Annualized Return  0.037828
4    Equal Weights  Annualized Return  0.083212

# plot dfl
sns.boxplot(x='Weighting Method', y='value', data=dfl, showfliers=False, color='tomato', hue='variable')
plt.legend(bbox_to_anchor=(1.04,0.5), loc="center left", borderaxespad=0)

enter image description here

Upvotes: 2

Related Questions