How to use the return value from pandas.DataFrame.boxplot?

matplotlib, pandas

Solution

The docstring is misleading there. You should file an issue on the Github page.

Keep in mind that `df.boxplot` takes a `rot` argument, so you can specify that at creation time. Unfortunately, it doesn't look like boxplot returns the axes:

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>]}

You can get ahold of the axes by selecting one of the lines and calling the `get_axes()` method:

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

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

And go on to do your formatting from there.

Problem

The pandas documentation tells me that pandas.DataFrame.boxplot() returns a matplotlib.axes.AxesSubplot but I seem to get a dict... am I misreading documentation or somehow otherwise confused? I'd like to be able to alter my axes labels, probably angling them since the current text is a little long and unwieldy. I can see that I could do this entirely in matplotlib but was wondering if I could use the return value of boxplot() to do this in pandas also? Code: ``` ax_subpl = d.boxplot(SHARING_FIELDS, grid=False) print(type(ax_subpl)) ``` Output: ``` <class 'dict'> ``` Thanks.

Original source