Python:使用matplotlib,在返回时如何获得箱线图

2024-04-19 17:06:26 发布

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

我有一个箱线图,但我想定制它的外观,所以我给出了箱线图方法参数return_type='both',以获得轴和字典。我似乎再也抓不出盒子了

不带return_type=True的代码:

df = pd.DataFrame(
            columns=['mean_coverage', 'exon'], 
            data= {
               'mean_coverage': [102, 578, 75, 180],
               'exon': [1, 1, 2, 2]
             }
           )

bp = df.boxplot(
        column='mean_coverage',
        by='exon',
        patch_artist=True,
    )

bp.figure.show()
bp.figure.savefig(
    os.path.join(
        self.output_path,
        'boxplot.png'
    )
)

return_type=True编码:

bp = df.boxplot(
    column='mean_coverage',
    by='exon',
    patch_artist=True,
    return_type='both'
)

for row_key, (ax,row) in bp.iteritems():
    for box in row['boxes']:
        box.set_facecolor('pink')

bp.figure.show()
bp.figure.savefig(
    os.path.join(
        self.output_path,
        'ZRSR2.png'
    )
)

AttributeError: 'Series' object has no attribute 'figure'

如何从该系列中获得如示例1所示的方框图