Skip to content

Cannot update boxplot via axes #4636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jorisvandenbossche opened this issue Aug 22, 2013 · 4 comments
Closed

Cannot update boxplot via axes #4636

jorisvandenbossche opened this issue Aug 22, 2013 · 4 comments
Labels
Milestone

Comments

@jorisvandenbossche
Copy link
Member

When I construct a boxplot with pandas, e.g. like this:

df = pd.DataFrame({'A':[1,3,2,5,4,7,6], 'B':[1,1,1,1,2,2,2]})
fig, ax = plt.subplots()
df.boxplot('A', 'B', ax=ax)

I cannnot change the plot anymore via ax, e.g. ax.set_ylim(0,20) or ax.set_ylabel("Something") has no effect on the figure.

Complete example: http://nbviewer.ipython.org/5868420/bug_pandas-boxplot-set-ylim.ipynb

I tested it on the dev version of pandas (and I know it worked previously, but cannot check which version exactly anymore).

@jorisvandenbossche
Copy link
Member Author

It seems that if you do

ax = df.boxplot('A', 'B', ax=ax)

instead of just

df.boxplot('A', 'B', ax=ax)

that you then can update the figure via ax. So maybe the axes object is changed in the boxplot function?

@fonnesbeck
Copy link

The solution suggested by @jorisvandenbossche does not appear to work, at least as of 0.13.0rc1-64-gceec8bf. I get the following:

AttributeError                            Traceback (most recent call last)
<ipython-input-51-f438708091a3> in <module>()
      1 fig, ax = plt.subplots()
      2 ax = df.boxplot(ax=ax, grid=False)
----> 3 ax.set_xlabel('Patient')

AttributeError: 'dict' object has no attribute 'set_xlabel'

@TomAugspurger
Copy link
Contributor

So boxplot() is returning a dict if things are grouped (the column and by arguments), and an axes if there's no grouping? That doesn't seem great.

@fonnesbeck as a bad workaround, you could grab the axes that the parts are drawn on:

In [150]: fig, ax = plt.subplots()

In [151]: ax = df.boxplot(ax=ax, grid=False)

In [152]: ax
Out[152]: 
{'fliers': [<matplotlib.lines.Line2D at 0x10bde7850>,
  <matplotlib.lines.Line2D at 0x10bdea910>,
  <matplotlib.lines.Line2D at 0x10bdf9c90>,
  <matplotlib.lines.Line2D at 0x10bdfdd50>],
 'caps': [<matplotlib.lines.Line2D at 0x10bddedd0>,
  <matplotlib.lines.Line2D at 0x10bdda0d0>,
  <matplotlib.lines.Line2D at 0x10bdefa50>,
  <matplotlib.lines.Line2D at 0x10bdf4610>],
 'whiskers': [<matplotlib.lines.Line2D at 0x10bdde710>,
  <matplotlib.lines.Line2D at 0x10bdc7190>,
  <matplotlib.lines.Line2D at 0x10bdea9d0>,
  <matplotlib.lines.Line2D at 0x10bdef4d0>],
 'medians': [<matplotlib.lines.Line2D at 0x10bde7110>,
  <matplotlib.lines.Line2D at 0x10bdf9410>],
 'boxes': [<matplotlib.lines.Line2D at 0x10bdd7b10>,
  <matplotlib.lines.Line2D at 0x10bdf4b50>]}

In [153]: real_ax = ax['fliers'][0].get_axes()

In [154]: real_ax.set_xlabel("Patient")
Out[154]: <matplotlib.text.Text at 0x10be51710>

@cpcloud
Copy link
Member

cpcloud commented Jun 5, 2014

closing this ... as it works in all examples shown. if i have some time i'll see if i can find what commit fixes this.

@cpcloud cpcloud closed this as completed Jun 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants