修改pandas箱线图输出

spo*_*234 1 python matplotlib pandas

根据文档,我在 pandas 中制作了这个图:

import pandas as pd
import numpy as np
import pyplot as plt

df = pd.DataFrame(np.random.rand(140, 4), columns=['A', 'B', 'C', 'D'])
df['models'] = pd.Series(np.repeat(['model1','model2', 'model3', 'model4', 'model5', 'model6', 'model7'], 20))
plt.figure()
bp = df.boxplot(by="models")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我怎样才能修改这个情节?

我想:

  • 将排列从 (2,2) 修改为 (1,4)
  • 更改标签和标题、文本和字体大小
  • 删除“[模型]”文本

以及如何将此图另存为 pdf ?

Ken*_*ste 7

  • 供安排使用layout
  • 用于设置 x 标签使用set_xlabel('')
  • 对于图标题使用figure.subtitle()
  • 要更改图形尺寸,请使用figsize=(w,h)(英寸)

注意:该行将np.asarray(bp).reshape(-1)子图的布局(例如 2x2)转换为数组。

代码 :

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

df = pd.DataFrame(np.random.rand(140, 4), columns=['A', 'B', 'C', 'D'])
df['models'] = pd.Series(np.repeat(['model1','model2', 'model3', 'model4', 'model5', 'model6', 'model7'], 20))
bp = df.boxplot(by="models",layout=(4,1),figsize=(6,8))
[ax_tmp.set_xlabel('') for ax_tmp in np.asarray(bp).reshape(-1)]
fig = np.asarray(bp).reshape(-1)[0].get_figure()
fig.suptitle('New title here')
plt.show()
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述