如何将BoxPlot打印到单个轴

2024-04-19 16:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三种不同的箱线图:

enter image description here

我用以下代码绘制:

import pandas as pd
import numpy as np

data_dict = {'Best fit': [395.0, 401.0, 358.0, 443.0, 357.0, 378.0, 356.0, 356.0, 403.0, 380.0, 397.0, 406.0, 409.0, 414.0, 350.0, 433.0, 345.0, 376.0, 374.0, 379.0, 9.0, 13.0, 10.0, 13.0, 16.0, 12.0, 6.0, 11.0, 20.0, 10.0, 12.0, 11.0, 15.0, 11.0, 11.0, 11.0, 15.0, 10.0, 8.0, 18.0, 864.0, 803.0, 849.0, 858.0, 815.0, 856.0, 927.0, 878.0, 834.0, 837.0, 811.0, 857.0, 848.0, 869.0, 861.0, 820.0, 887.0, 842.0, 834.0, np.nan], 'MDP': [332, 321, 304, 377, 304, 313, 289, 314, 341, 321, 348, 334, 361, 348, 292, 362, 285, 316, 291, 318, 3, 6, 5, 5, 4, 5, 4, 3, 8, 6, 4, 0, 8, 1, 4, 0, 9, 5, 3, 8, 770, 770, 819, 751, 822, 842, 758, 825, 886, 830, 774, 839, 779, 821, 812, 850, 822, 786, 874, 831], 'Q-Learning': [358, 329, 309, 381, 302, 319, 296, 315, 343, 318, 338, 336, 360, 357, 299, 363, 287, 337, 301, 334, 3, 6, 5, 5, 4, 5, 4, 3, 8, 6, 4, 0, 8, 1, 4, 0, 9, 5, 3, 8, 771, 833, 757, 837, 831, 784, 806, 890, 843, 775, 838, 776, 824, 830, 834, 827, 791, 868, 816, 806], 'parametrized_factor': [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, np.nan]}
data = pd.DataFrame(data_dict)

ax = data.boxplot(column=['Best fit', 'MDP', 'Q-Learning'], by='parametrized_factor', 
              showfliers=True,
              grid=True, 
              positions=[0.2, 1.0, 2.0],
              figsize=(12,8), 
              showmeans=True, 
              meanprops={"marker": "+", "markeredgecolor": "black", "markersize": "10"}, 
              medianprops={"marker": ".", "markeredgecolor": "red", "markersize": "10"},
              widths=0.4,
              showbox=True
            )

如何在同一个绘图下绘制所有三个绘图


1条回答
网友
1楼 · 发布于 2024-04-19 16:28:25
  • 数据帧可以用^{}融化成长格式,然后用^{}^{}绘制,并指定hue参数
import seaborn as sns

# melt the dataframe into a long form
dfm = data.melt(id_vars='parametrized_factor')

# plot
ax = sns.boxplot(data=dfm, x='variable', y='value', hue='parametrized_factor')
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')

enter image description here

sns.catplot(kind='box', data=dfm, x='variable', y='value', hue='parametrized_factor')

enter image description here

相关问题 更多 >