【发布时间】:2018-11-30 12:23:24
【问题描述】:
这是参考以下问题,其中讨论了调整子图标题和布局的选项: modify pandas boxplot output
我的要求是更改每个子图中各个框的颜色(如下所示):
以下是共享链接中用于调整子图的标题和轴属性的代码:
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()
我尝试使用: ax.set_facecolor('颜色') 属性,但没有成功获得想要的结果。
我也尝试访问 bp['boxes'] 但显然它不可用。我需要了解存储在 bp 中的数据结构,才能访问子图中的各个框。
期待
P.S:我知道 seaborn。但目前需要使用 df.boxplot 来理解和实现。谢谢
【问题讨论】:
-
我认为使用 pandas 很难做到这一点。由于 pandas 绘图在引擎盖下使用
matplotlib,我会考虑使用纯 matplotlib,这使得更改单个框的颜色变得非常容易
标签: python pandas dataframe matplotlib boxplot