与Pandas并排的箱田

2024-06-15 03:10:49 发布

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

我需要绘制保存在pandasdataframe中的五个变量的比较图。我用了一个例子from here,它起作用了,但现在我需要更改坐标轴和标题,但我正在努力做到这一点。在

以下是我的数据:

df1.groupby('cls').head()
Out[171]: 
   sensitivity  specificity  accuracy       ppv       auc       cls
0     0.772091     0.824487  0.802966  0.799290  0.863700       sig
1     0.748931     0.817238  0.776366  0.785910  0.859041       sig
2     0.774016     0.805909  0.801975  0.789840  0.853132       sig
3     0.826670     0.730071  0.795715  0.784150  0.850024       sig
4     0.781112     0.803839  0.824709  0.791530  0.863411       sig
0     0.619048     0.748290  0.694969  0.686138  0.713899  baseline
1     0.642348     0.702076  0.646216  0.674683  0.712632  baseline
2     0.567344     0.765410  0.710650  0.665614  0.682502  baseline
3     0.644046     0.733645  0.754621  0.683485  0.734299  baseline
4     0.710077     0.653871  0.707933  0.684313  0.732997  baseline

这是我的代码:

^{pr2}$

结果是:

pictures of results

如何:

  • 更改标题('box plot grouped by cls')
  • 去掉那些沿着水平线绘制的烦人的[cls]
  • 重新排列df1中显示的打印类别?(首先是灵敏度,然后是speci…)

Tags: 数据from标题here绘制outhead例子
2条回答

我建议使用seaborn

下面是一个可能有助于您的示例:

进口

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

制作数据

^{pr2}$

Seaborn有一个很好的工具,叫做factorplot,它创建了一个子空间网格,其中的行/列是用您的数据构建的。为了做到这一点,我们需要将df融化成一个更有用的形状。在

df_melt = df.melt(id_vars = 'cls',
                  value_vars = ['accuracy',
                                'auc',
                                'ppv',
                                'sensitivity',
                                'specificity'],
                  var_name = 'columns')

现在我们可以使用列“columns”创建factorplot。在

a = sns.factorplot(data = df_melt,
                   x = 'cls',
                   y = 'value',
                   kind = 'box', # type of plot
                   col = 'columns',
                   col_order = ['sensitivity', # custom order of boxplots
                                'specificity',
                                'accuracy',
                                'ppv',
                                'auc']).set_titles('{col_name}') # remove 'column = ' part of title

plt.show()

factorplot

你也可以使用Seaborn的箱线图。在

b = sns.boxplot(data = df_melt,
                hue = 'cls', # different colors for different 'cls'
                x = 'columns',
                y = 'value',
                order = ['sensitivity', # custom order of boxplots
                         'specificity',
                         'accuracy',
                         'ppv',
                         'auc'])

sns.plt.title('Boxplot grouped by cls') # You can change the title here
plt.show()

boxplot

这将给你相同的情节,但所有在一个数字,而不是子情节。它还允许您用一行更改图形的标题。不幸的是,我找不到一个方法删除'列'副标题,但希望这将得到你所需要的。在

编辑

要从侧面查看绘图: 工厂生产线 交换您的xy值,将col = 'columns'更改为row = 'columns',将{}更改为{},并将'{col_name}'更改为'{row_name}'

a1 = sns.factorplot(data = df_melt,
                    x = 'value',
                    y = 'cls',
                    kind = 'box', # type of plot
                    row = 'columns',
                    row_order = ['sensitivity', # custom order of boxplots
                                 'specificity',
                                 'accuracy',
                                 'ppv',
                                 'auc']).set_titles('{row_name}') # remove 'column = ' part of title

plt.show()

h factorplot 箱形图 交换xy值,然后添加参数orient = 'h',如下所示

b1 = sns.boxplot(data = df_melt,
                 hue = 'cls',
                 x = 'value',
                 y = 'columns',
                 order = ['sensitivity', # custom order of boxplots
                         'specificity',
                         'accuracy',
                         'ppv',
                         'auc'],
                 orient = 'h')

sns.plt.title('Boxplot grouped by cls')
plt.show()

h boxplot

也许这对你有帮助:

fig, axes = pyplot.subplots(ncols=4, figsize=(12, 5), sharey=True)
df.query("E in [1, 2]").boxplot(by='E', return_type='axes', ax=axes, column=list('bcda')) # Keeping original columns order
pyplot.suptitle('Boxplot') # Changing title
[ax.set_xlabel('') for ax in axes] # Changing xticks for all plots

相关问题 更多 >