2

I am running this notebook: https://github.com/cs109/2014/blob/master/labs/Lab2_Notes.ipynb

I have not edited the notebook (except for removing pd.options.display.mpl_style = 'default' in the very first chunk, which was causing errors while plotting).

At the part where it asks for a scatterplot of price and carat grouped by color:

diamonds.groupby('color').plot(kind = 'scatter', x = 'carat', y = 'price',
 color = 'black', alpha = 1)

when I run it, I get a plot like this:

enter image description here

All 7 scatter plots look similar to this. I tried changing it to boxplot, but it made no difference, it still returns the same plot. In fact, it doesn't seem to matter what I use for the kind parameter, I'm always getting the same graph (tried pie, hist, hexbin, etc.).

What is going wrong? I'm running pandas version 0.17.1 and matplotlib version 1.5.0.

Community
  • 1
  • 1
ytk
  • 2,787
  • 4
  • 27
  • 42
  • The `.plot(kind='scatter', ..)` is not implemented for `groupby` objects. You will have to iterate through the groups: `for group in diamonds.groupby('color'): group.plot(..)` – joris Dec 07 '15 at 08:53
  • @joris, thank you for the explanation. When I try it, I am getting the following error: `AttributeError: 'tuple' object has no attribute 'plot'`. Also, out of curiosity, do you have any idea how they managed to make it work in the initial notebook? Is it because they are using a different version of pandas? – ytk Dec 07 '15 at 09:22
  • Ah, sorry, I was mistaken. This did indeed work (tested in 0.16.2), but not anymore in 0.17.1. Seems like a regression! (do you want to report it? https://github.com/pydata/pandas/issues) – joris Dec 07 '15 at 11:21
  • For a workaround: the iterating approach should work, but `for name, group in df.groupby('color'): ..` is needed, sorry for that. See http://pandas.pydata.org/pandas-docs/stable/groupby.html#iterating-through-groups – joris Dec 07 '15 at 11:22
  • Another workaround is to use `diamonds.groupby('color').plot.scatter(x = 'carat', y = 'price')` – joris Dec 07 '15 at 12:32
  • @joris, thank you. I got it to work now! [This](http://stackoverflow.com/questions/21654635/scatter-plots-in-pandas-pyplot-how-to-plot-by-category) thread was also pretty helpful. However, I went with the workaround you suggested because it was simpler. – ytk Dec 07 '15 at 23:07

0 Answers0