pydata/pandas — pull request #6678 by sinhrks (2014-03-21 07:30:24+00:00)
Title: BUG: legend behaves inconsistently when plotting to the same axes 

There seems to be some inconsistencies related to `DataFrame.plot` and `Series.plot` legend behaviors.

*Problems:*
- When `DataFrame.plot` or `Series.plot` plots data on the same axes repeatedly:

- If the target axes already has a legend, line plot always appends its legend to existing one ignoring `legend` kw and existing legend will be drawn as line artist regardless of actual artist type. Also, legend cannot be `reverse`ed if the axes already has a legend.
- Bar/BarH plot deletes the existing legend and overwrites with latest one.
- KDE plot appends its legend to the existing one, and will apply `reverse` to all artists including the existing one.
- When `subplots` is enabled, line plot draws legends on each axes but bar plot doesn't.
- Scatter plot does not draw legend even if `label` keyword is passed.

*Fix:*
I've prepared a fix based on following concept, except `hexbin` which doesn't use legend.
- Legend should be drawn by the order of the artists drawn.
- Each legend should be drawn according to the passed `legend` value.
- If df1 plots with `legend=True` and df2 with `legend=False`, only df1's legend should appear.
- If df2 plots with `legend='reverse'`d, only df2's legend should be reversed.
- When `subplots=True` and `legend=True`, each subplot axes should have its own legend (standardize current line plot behavior).

*Example Code*
```
df1 = DataFrame(randn(6, 3), index=range(6), columns=['a', 'b', 'c'])
df2 = DataFrame(randn(6, 3), index=range(6), columns=['d', 'e', 'f'])
df3 = DataFrame(randn(6, 3), index=range(6), columns=['x', 'y', 'z'])

fig, axes = plt.subplots(1, 5, figsize=(14, 3))
for i, (legend, df) in enumerate(zip([True, False, 'reverse'], [df1, df2, df3])):
df.plot(ax=axes[0], legend=legend, title='line')
df.plot(kind='bar', ax=axes[1], legend=legend, title='bar')
df.plot(kind='barh', ax=axes[2], legend=legend, title='barh')
df.plot(kind='kde', ax=axes[3], legend=legend, title='kde')
df.plot(kind='scatter', ax=axes[4], x=df.columns[0], y=df.columns[1], label=i,
legend=legend, title='scatter')
plt.show()
```

*Output using current repository*
- For line, bar, kde plot, expected legend is `a, b, c, z, y x`. Because df2 was plot by `legend=False`, and df3 was plot by `legend='reverse'`.
- For scatter plot, `data1, data3` is expected because df2 was plot by `legend=False`.

![figure_legend_current](https://f.cloud.github.com/assets/1696302/2480801/ba3c694a-b0c9-11e3-8f5b-d2ebec0798e3.png)

*Output after fix*
![figure_fixed](https://f.cloud.github.com/assets/1696302/2480773/b1bd8f70-b0c8-11e3-9654-3e3c2b20157b.png)

If there is anything should be considered, please let me know. Thanks.