Skip to content
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

Plotting in a specific axis with DataFrame.hist(by=) is not working as expected #5353

Closed
joelostblom opened this issue Oct 27, 2013 · 8 comments · Fixed by #7736
Closed

Plotting in a specific axis with DataFrame.hist(by=) is not working as expected #5353

joelostblom opened this issue Oct 27, 2013 · 8 comments · Fixed by #7736
Milestone

Comments

@joelostblom
Copy link
Contributor

Hi,
I posted this on stackoverflow http://stackoverflow.com/questions/19602864/how-to-plot-in-a-specific-axis-with-dataframe-histby-in-pandas?noredirect=1#comment29118427_19602864 and was suggested to file a bug report here.

I am trying plot several histogram groups in the same figure. Each group contains two conditions and I am therefore using the 'by=' argument from pandas histogram options. However, this does not work as I expected and pandas creates a new figure instead of plotting in the axis I am passing. I tried to pass four axes as well, but still no go. Sample code:

import pandas as pd
df = pd.DataFrame({'color': ['blue','blue','yellow','blue','yellow'], 'area': [2,2,3,4,4]})
fig, (ax1, ax2) = plt.subplots(1,2)
df.area.hist(by=df.color, ax=ax1)

I'm using pandas 0.12.0, matplotlib 1.3.0, numpy 1.7.1 and python 2.7.5. Any suggestion that leads to a way of combining/stitching multiple 'hist(by=)-plots' in the same subplot grid is welcome.

@jreback
Copy link
Contributor

jreback commented Apr 9, 2014

@TomAugspurger can you have a look. still a bug?

@cpcloud
Copy link
Member

cpcloud commented Apr 10, 2014

This works by doing

df.hist(column='area', by=df.color)

and it returns an array of AxesSubplot objects. There isn't really an API for passing in multiple axes.

@cpcloud
Copy link
Member

cpcloud commented Apr 10, 2014

@Cheflo As long as your data are in the same frame, why do you need to pass axes in? If you need to manipulate the axes, why not do it after the fact?

@jreback Should we have an API for passing in multiple axes? Related is #6769.

@jreback
Copy link
Contributor

jreback commented Apr 10, 2014

umm, I guess, how does seaborn / ggplot do it? I suppose it might be nice to map it like

{ 'A' : [0,1], 'B' : [1,1], 'C' : [0,1]} for which column to put in the generated subplots / axes

but seems kind of complicated to me

@jreback
Copy link
Contributor

jreback commented Apr 10, 2014

going to move this and the other one you pointed to 0.15

@jreback jreback modified the milestones: 0.15.0, 0.14.0 Apr 10, 2014
@cpcloud
Copy link
Member

cpcloud commented Apr 10, 2014

IMO pandas plotting code should just be maintenance and bug fix only

@jreback
Copy link
Contributor

jreback commented Apr 10, 2014

yep, although a couple of new contributors have add some good stuff: http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#plotting

@joelostblom
Copy link
Contributor Author

Thanks for looking into this.

If I remember correctly, I had a figure with something else plotted in one subplot and wanted to be able to direct the histogram from pandas into one of the empty subplots of that figures. Maybe this code describes my situation better:

import pandas as pd
df = pd.DataFrame({'color': ['blue','blue','yellow','blue','yellow'], 'area': [2,2,3,4,4]})
#fig, (ax1, ax2) = plt.subplots(1,2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
ax3.plot([[2,2], [3,6]])
ax4.plot([[3,6], [2,2]])
df.area.hist(by=df.color, ax=ax1)

Ideally, in my example, the pandas histogram is 1,2 and should then split ax1 into two subplots. Alternatively, it could be plotted into ax1 and ax2, and then the user could make sure that the correct number of empty subplots are available.

This is just how I thought it would work when I saw the axis argument and it would be useful for me. However, if this is overly time consuming to implement, there are obviously far more important things to focus on than adjusting minor plotting features as already mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants