如何使用pandas.DataFrame.boxplot的返回值?

时间:2013-12-08 12:07:43

标签: matplotlib pandas

pandas documentation告诉我pandas.DataFrame.boxplot()返回一个matplotlib.axes.AxesSubplot,但我似乎得到了一个字典...我是在误读文档还是以其他方式混淆了?

我希望能够改变我的轴标签,可能会对它们进行调整,因为当前文本有点长而且不实用。 I can see that I could do this entirely in matplotlib但是想知道我是否可以使用boxplot()的返回值在pandas中执行此操作?

代码:

ax_subpl = d.boxplot(SHARING_FIELDS, grid=False)
print(type(ax_subpl))

输出:

<class 'dict'>

感谢。

1 个答案:

答案 0 :(得分:4)

文档字符串在那里有误导性。您应该在Github page上提交问题。

请记住df.boxplot采用rot参数,因此您可以在创建时指定它。 不幸的是,它看起来不像boxplot返回轴:

In [45]: df = DataFrame(rand(10,5))

In [46]: bp = df.boxplot(rot=45)

In [47]: bp
Out[47]: 
{'boxes': [<matplotlib.lines.Line2D at 0x111f53a50>,
  <matplotlib.lines.Line2D at 0x111f5dc10>,
  <matplotlib.lines.Line2D at 0x111f68e50>,
  <matplotlib.lines.Line2D at 0x111f740d0>,
  <matplotlib.lines.Line2D at 0x111f7d310>],
 'caps': [<matplotlib.lines.Line2D at 0x111f4eb50>,
  <matplotlib.lines.Line2D at 0x111f4ecd0>,
  <matplotlib.lines.Line2D at 0x111f5af50>,
  <matplotlib.lines.Line2D at 0x111f5d5d0>,
  <matplotlib.lines.Line2D at 0x111f681d0>,
  <matplotlib.lines.Line2D at 0x111f68810>,
  <matplotlib.lines.Line2D at 0x111f72410>,
  <matplotlib.lines.Line2D at 0x111f72a50>,
  <matplotlib.lines.Line2D at 0x111f7a650>,
  <matplotlib.lines.Line2D at 0x111f7ac90>],
 'fliers': [<matplotlib.lines.Line2D at 0x111f58710>,
  <matplotlib.lines.Line2D at 0x111f5a110>,
  <matplotlib.lines.Line2D at 0x111f608d0>,
  <matplotlib.lines.Line2D at 0x111f60ed0>,
  <matplotlib.lines.Line2D at 0x111f6bb10>,
  <matplotlib.lines.Line2D at 0x111f6e510>,
  <matplotlib.lines.Line2D at 0x111f74d50>,
  <matplotlib.lines.Line2D at 0x111f77750>,
  <matplotlib.lines.Line2D at 0x111f7df90>,
  <matplotlib.lines.Line2D at 0x111f80990>],
 'medians': [<matplotlib.lines.Line2D at 0x111f580d0>,
  <matplotlib.lines.Line2D at 0x111f60290>,
  <matplotlib.lines.Line2D at 0x111f6b4d0>,
  <matplotlib.lines.Line2D at 0x111f74710>,
  <matplotlib.lines.Line2D at 0x111f7d950>],
 'whiskers': [<matplotlib.lines.Line2D at 0x111f4eed0>,
  <matplotlib.lines.Line2D at 0x111f4e7d0>,
  <matplotlib.lines.Line2D at 0x111f5a710>,
  <matplotlib.lines.Line2D at 0x111f5a910>,
  <matplotlib.lines.Line2D at 0x111f638d0>,
  <matplotlib.lines.Line2D at 0x111f63b50>,
  <matplotlib.lines.Line2D at 0x111f6eb10>,
  <matplotlib.lines.Line2D at 0x111f6ed90>,
  <matplotlib.lines.Line2D at 0x111f77d50>,
  <matplotlib.lines.Line2D at 0x111f77fd0>]}

您可以通过选择其中一行并调用get_axes()方法来获取轴:

In [48]: ax = bp['boxes'][0].get_axes()

In [49]: ax
Out[49]: <matplotlib.axes.AxesSubplot at 0x111ed1f50>

然后从那里继续进行格式化。