Repository: bokeh/bokeh

Issue:
[BUG] Legend order backwards for stacked area plots
#### ALL software version info (bokeh, python, notebook, OS, browser, any other relevant packages)
Python 3.7.1
bokeh 1.0.2

#### Description of expected behavior and the observed behavior
Enabling the legend in a stacked area chart displays the items in the opposite order of how they are stacked. One would expect them to be in the same order.

#### Complete, minimal, self-contained example code that reproduces the issue
This is the code from https://bokeh.pydata.org/en/latest/docs/gallery/stacked_area.html with the legend added.

```python
import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer

N = 20
cats = 10
df = pd.DataFrame(np.random.randint(10, 100, size=(N, cats))).add_prefix('y')

def stacked(df):
    df_top = df.cumsum(axis=1)
    df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1]
    df_stack = pd.concat([df_bottom, df_top], ignore_index=True)
    return df_stack

areas = stacked(df)
colors = brewer['Spectral'][areas.shape[1]]
x2 = np.hstack((df.index[::-1], df.index))

p = figure(x_range=(0, N-1), y_range=(0, 800))
p.grid.minor_grid_line_color = '#eeeeee'

for c, color in zip(areas, colors):
    p.patch(x2, areas[c].values, legend=c+' ',
          color=color, alpha=0.8, line_color=None)
p.legend.location = "top_left"
p.legend.click_policy = "hide"

show(p)
```

#### Screenshots or screencasts of the bug in action

<img width="519" alt="image" src="https://user-images.githubusercontent.com/14860330/57477859-9516ad00-7267-11e9-9825-3feccd329034.png">



Discussion:
@bdwiel thank you for your comment. I think this is simply a usage question. The legends have to be rendered in *some* default order, and no default order is going to satisfy everyone in every situation. In most cases (e.g lines or scatters) there is no inherent visual/directional ordering at all, and I think the default of having legend entries descend in the order they were added makes sense. If you want something different, you always have full control to re-order the legend items however you want, e.g

```
p.legend[0].items.reverse()
```

right before calling `show`
I agree that there isn't a best solution in every situation, but for the specific situation of stacked charts having the bottom entry correspond to the bottom of the stack is more intuitive because a stacked chart does have an order.

Thanks for the tip about reversing the legend. That is fine for my needs. Perhaps the example can include it because I'm afraid it would have taken a lot of searching to find that solution for someone beginning to use Bokeh.
> Perhaps the example can include it because I'm afraid it would have taken a lot of searching to find that solution for someone beginning to use Bokeh.

I definitely think that would be helpful as well, would you be interested in creating a small PR to update the example? 

Resolving patch:
diff --git a/examples/plotting/file/stacked_area.py b/examples/plotting/file/stacked_area.py
--- a/examples/plotting/file/stacked_area.py
+++ b/examples/plotting/file/stacked_area.py
@@ -21,8 +21,12 @@ def stacked(df):
 p = figure(x_range=(0, N-1), y_range=(0, 800))
 p.grid.minor_grid_line_color = '#eeeeee'
 
-p.patches([x2] * areas.shape[1], [areas[c].values for c in areas],
-          color=colors, alpha=0.8, line_color=None)
+for c, color in zip(areas, colors):
+    p.patch(x2, areas[c].values, legend=c,
+            color=color, alpha=0.8, line_color=None)
+
+# reverse the legend entries to match the stacked order
+p.legend[0].items.reverse()
 
 output_file('stacked_area.html', title='stacked_area.py example')
 


Test patch: