如何在 Python 中将两个具有相同轴的箱线图合并为一个箱线图

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

如何将两个轴相同的独立箱线图组合成一个箱线图?所有数据都来自同一个数据框。

我有两张图,我想将它们合二为一:

图 1)

图 2)

如何组合它们,使它们看起来像这样(类似于使用 hue 参数时):

我当前的数据框如下所示。请注意,我手动添加了一个 'Data Type' 列,以便我可以使用 sns.boxplot 中的 hue 参数来展示我的示例。 'Data Type' 列不在实际数据框中:

         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

这是我用来生成所需输出的代码。同样,手动添加 hue 参数只是为了可视化目的:

sns.boxplot(x='Weighting Method',y = 'Annualized Volatility',data=df,showfliers=False,color='tomato',hue='Data Type')
  • 数据帧需要从宽格式转换为长格式
  • 使用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)