ENH: Scatterplot Method added
State: closed

closes #2215

This is a simple pull request that creates a shortcut for scatter plots from a df.  This is in reference to  #2215  (and  #1527) and follows the same pattern as df.hist(). 

```
#where you would previously have had to do:
from pandas.tools.plotting import scatter_plot 
scatter_plot(df, x = "x", y = 'y')

#you can now do
df.scatter(x="x",y="y")

#example
import pandas as pd
import numpy as np
df = pd.DataFrame( np.random.randn(100,4))
df.scatter(x=1,y=2)
```

I followed the same pattern as df.hist() even though it looks like you are intended make a class object for scatterplots that can be passed to the plot_frame() function as is the case for BarPlot and LinePlot. In any case, this works and may be useful.

best,
zach cp


Labels:

Comments:
Adding one method per type of plot would pollute the DataFrame namespace too much.
Can't this be rolled in to df.plot(x=,y=,kind='scatter') in some way?

@cpcloud @jtratner is this how we are handling the api for this? (`kind='scatter'`) seems good to me?

@cpcloud seems like could include this in 0.13?

@zachcp can you rebase this?

i'll take a look

echoing @y-p, i'd rather have `kind='scatter'` than another plotting method 

+1 to kind='scatter'

On Sat, Sep 21, 2013 at 8:44 PM, Phillip Cloud notifications@github.comwrote:

> echoing @y-p https://github.com/y-p, i'd rather have kind='scatter'than another plotting method
> 
> —
> Reply to this email directly or view it on GitHubhttps://github.com/pydata/pandas/pull/3473#issuecomment-24873591
> .

I'm relatively new to git and working through the rebase process.  I may have just tangled up my code during the rebase process (grrrr.....) so perhaps I  will just fork master and start over. 

do this:

``` sh
git remote add upstream git://github.com/pydata/pandas.git # if you haven't already
git checkout master
git fetch upstream
git rebase upstream/master
git checkout --branch zachcp-awesome-branch-of-greatness
git cherry-pick 485ac8b 3d2819d 7aff758
```

Thanks @cpcloud. I almost actually typed `zachcp-awesome-branch-of-greatness`. Its probably more like `twisted-branch-of-attempting`.

With the code in pull request #4930  I can do this:

```
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(50,4), columns= ['a','b','c','d'])
df.plot(x='a',y='b', kind='scatter')
```

not sure how elegant the code is but it works!

@zachcp need to rebase to master

```
git rebase origin/master
```

then you can 

```
git rebase -i <before your fist commit>
```

to squash(s) down to 1 commit

then

```
git push yourfork branchname -f
```

@jreback I've been working on PR #4930 with @cpcloud's help. The PR is running on Travis CI although I may need better tests.

fyi...is #4493 related (can you add a test for this)?

@jreback no that's a matplotlib version issue

related to matplotlib's use of the `mask` attribute of numpy's masked arrays. since we define a method called `mask` you get "correct" behavior, but not on purpose (so really it's _not_ correct)

@cpcloud ok....thxs

@zachcp looks like u forgot to remove some merge conflict markers, i.e., things like `<<<<< HEAD`.

gotta keep rebasing ... sorry about that ... things are moving quickly around here :smile: 

@zachcp can you squash this down to 1 or 2 commits? thanks

hmm. squashing didn't work quite as planned.....  master is a moving target for rebasing.

You're probably just misunderstanding how rebasing works.

Do this:

``` sh
# go to master
git checkout master

# get the latest upstream
git fetch upstream
git rebase upstream/master

# new branch to put your commits
git checkout -b new-plotting-branch

# now for the fun stuff
git rev-list --reverse upstream/master..scatterplot --author="$(git config user.name)" | while read commit; do
    git cherry-pick $commit
done
```

Okay, you've still got some history in there. Do the following:

``` sh
git checkout scatterplot
git reset HEAD~1
git stash
git fetch upstream
git rebase upstream/master
git reset --hard upstream/master
git stash pop
git add .
git commit -m'YOUR AWESOME MESSAGE HERE'
```

`You're probably just misunderstanding how rebasing works.` - true,true.
@cpcloud  - Thank you for taking the time to help me remedy that.

It's important to learn how to use `git` when contributing on GitHub. I can only go so far in helping you with snippets and such. I'm not sure what the best way to learn git is. Personally, I didn't start understanding how it worked from reading a bunch of tutorials/rants about it on the web, I actually had to _produce things_ with it and thereby f**k up many, many times and figure out why I messed up.

Good thing about `git` is, it seems to know that you'll misuse (abuse?) it. It remembers practically everything you do which makes learning it slightly less stressful than if you weren't able to recover almost everything. I say almost, because of course git doesn't prevent you from shooting yourself in the foot, but then, why are you aiming at your foot in the first place? :smile: 

@zachcp I tried out your branch, and I think there are still some issues with it. When trying out some `df.plot(..., kind='scatter')` plots, I encountered following issues:
- The `subplots=True` keyword does not seem to do something useful (although you tested for it in the test). With the example in the test I get three identical subplots. E.g. with `kind='bar'`, this keyword has no effect when both `x` and `y` are specified.
- When trying `df.plot(kind='scatter')` (so without specifying `x` and `y`, I get the error message "UnboundLocalError: local variable 'ser' referenced before assignment", because here https://github.com/zachcp/pandas/blob/30da599b27fa5d26236ccc03fce439ead87567a3/pandas/tools/plotting.py#L1676 `ser` is not known. 
- The same goes for when only `x` is specified, and only specifying `y` also yields an (other) error.
- The x and y label can be added as with the original `scatter_plot`.

I think you should try out some examples and some corner cases to test the code you are writing.

@zachp can you run flake8 on this file? It'll reveal many of these errors. 

@zachcp would like to get this in for 0.13... think u can get @jorisvandenbossche's issues addressed in the next 2 weeks-ish?

@cpcloud and @jorisvandenbossche 

I updated the scatter plot class and tests to address several of Joris's issues. You will raise a `ValueError`if you try to use `kind=scatter` without specifying both `x` and `y`. So at a minimum the scatterplot works. However there are a few things that we might want to include to get feature parity with the `scatter_plot()` function (as Joris has suggested):
- axis labels
- the `by` keyword for grouping sets of plots
- others?

Looking through the code it looks like plotting was developed in parallel with functions like `grouped_hist`, `scatter_plot`, and `scatter_matrix` sharing code while each plotting class/klass was written independently. I am  more familiar with the style used in the functions than with that used by the classes, but could take a stab at implementing some of these features.

In realistic terms I can probably get the axis labels incorporated in time. The logic for subplotting using the 'by' keyword (subplots) may take longer. 

@zachcp Thanks for the update! I added some other remarks/questions to the code.

@zachcp can you update according to the comments? almost there!

@jreback @jorisvandenbassche @cpcloud.  I'll have some free time tomorrow and will try to update/address everything then.

Updated code. The `ScatterPlot` class is now quite simple but functional. I test to see that it works and that both x and y values are passed.