Legend, brewer.py example

Hi,

How could I add the legend to the brewer.py example? I need it outside the area plot.

https://bokeh.pydata.org/en/latest/docs/gallery/brewer.html

Thanks!

Felipe

Hi Felipe,

Here is a working example doing that. Essentially I modded the example to work using a ColumnDataSource (in an extremely quick and dirty way), into which you can put the category labels (just the generated column names here). Then its fairly easy to get it to make
a legend for you. The simplest way I’ve found to get a legend outside of the plot area is to take a copy of it, remove the legend instance from the plot, and add it back in off to the side somewhere.
import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer
from bokeh.core.properties import value
from bokeh.models import ColumnDataSource

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]]
cols = areas.columns

x2 = np.hstack((df.index[::-1], df.index))

Quick and dirty to get it into a source format.

source = ColumnDataSource(dict(
xs = [x2] * areas.shape[1],
ys = [areas[c].values for c in areas],
label = cols,
color = colors,
))

p = figure(x_range=(0, N-1), y_range=(0, 800))
p.grid.minor_grid_line_color = ‘#eeeeee

p.patches(xs=‘xs’, ys=‘ys’, color=‘color’, legend=‘label’,
source=source, alpha=0.8, line_color=None)

First format the legend as desired.

p.legend.padding = 0
p.legend.border_line_color = None
p.legend.background_fill_alpha = 0

Then take a copy of it.

new_legend = p.legend[0]

Then remove it from the plot and add it back in off to the side.

p.legend[0].plot = None
p.add_layout(new_legend, ‘right’)

output_file(‘brewer.html’, title=‘brewer.py example’)

show§

``

Hope this helps,

Jack

···

On Tuesday, February 27, 2018 at 4:09:11 PM UTC+1, Felipe Cuevas wrote:

Hi,

How could I add the legend to the brewer.py example? I need it outside the area plot.

https://bokeh.pydata.org/en/latest/docs/gallery/brewer.html

Thanks!

Felipe

Hi Jack,

It works great!! it helped a lot and I got the idea how to do it…
thanks a lot for the working example and the explanation

Regards,

Felipe

···

2018-02-27 20:00 GMT-03:00 Jack Beanland [email protected]:

Hi Felipe,

Here is a working example doing that. Essentially I modded the example to work using a ColumnDataSource (in an extremely quick and dirty way), into which you can put the category labels (just the generated column names here). Then its fairly easy to get it to make
a legend for you. The simplest way I’ve found to get a legend outside of the plot area is to take a copy of it, remove the legend instance from the plot, and add it back in off to the side somewhere.
import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer
from bokeh.core.properties import value
from bokeh.models import ColumnDataSource

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]]
cols = areas.columns

x2 = np.hstack((df.index[::-1], df.index))

Quick and dirty to get it into a source format.

source = ColumnDataSource(dict(
xs = [x2] * areas.shape[1],
ys = [areas[c].values for c in areas],
label = cols,
color = colors,
))

p = figure(x_range=(0, N-1), y_range=(0, 800))
p.grid.minor_grid_line_color = ‘#eeeeee

p.patches(xs=‘xs’, ys=‘ys’, color=‘color’, legend=‘label’,
source=source, alpha=0.8, line_color=None)

First format the legend as desired.

p.legend.padding = 0
p.legend.border_line_color = None
p.legend.background_fill_alpha = 0

Then take a copy of it.

new_legend = p.legend[0]

Then remove it from the plot and add it back in off to the side.

p.legend[0].plot = None
p.add_layout(new_legend, ‘right’)

output_file(‘brewer.html’, title=‘brewer.py example’)

show§

``

Hope this helps,

Jack

On Tuesday, February 27, 2018 at 4:09:11 PM UTC+1, Felipe Cuevas wrote:

Hi,

How could I add the legend to the brewer.py example? I need it outside the area plot.

https://bokeh.pydata.org/en/latest/docs/gallery/brewer.html

Thanks!

Felipe

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/b42bc15b-ef04-4b30-b9dc-96c17e94f47c%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.