[bokeh] Bokeh legend not mapped to specified field in source

Hi,

My suspicion is that the interaction of transforms with "groupby" legends has never been explicitly considered, and so probably does not do anything desirable. A GH issue to request making these two features work better would be appropriate. As a workaround in the mean time, I can only suggest colormapping the data yourself in python (instead of using factor_cmap) and sending the colors as their own column.

Thanks,

Bryan

···

On Apr 14, 2018, at 11:08, [email protected] wrote:

I've created a bokeh app that displays birth data at the county and state levels between 1990 and 2007. The issue is that the legend I've defined doesn't display information for all of the factors in the specified column ('race'). Here is my source code:

from bokeh.io import output_notebook, output_file, show,
curdoc

from bokeh.plotting import
figure

from bokeh.models import ColumnDataSource, HoverTool, Slider, CDSView, BooleanFilter, NumeralTickFormatter
from bokeh.palettes import Spectral as
palette

from bokeh.layouts import widgetbox, row,
column

from bokeh.transform import
factor_cmap

source
= ColumnDataSource(data=dict(final_merge.reset_index()))

races
= ['black', 'hispanic', 'other', 'white']

def set_bools(yr, lvl):

return [True if (year == yr) and (level == lvl) else False for year, level in zip(source.data['year'], source.data['level'])]

def set_views(boolean):

return CDSView(source=source, filters=[BooleanFilter(boolean)])

p1
= figure(x_axis_label = 'Number of Births', y_axis_label = 'Percentage of Births',

                title
='Harris County Births 1990-2007', x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

p2
= figure(x_axis_label = 'Number of Births', y_axis_label = 'Percentage of Births',

                title
='Texas Births 1990-2007', x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

bools1
, bools2 = set_bools(1990, 'county'), set_bools(1990, 'state')

view1
, view2 = set_views(bools1), set_views(bools2)

c1
= p1.circle(x='total', y='percent', size=10, color=factor_cmap('race', palette[4], races), legend='race', source=source)
c2 = p2.circle(x='total', y='percent', size=10, color=factor_cmap('race', palette[4], races), legend='race', source=source)

c1
.view, c2.view = view1,
view2

p1
.legend.location = 'bottom_right'

p2
.legend.location = 'bottom_right'

hover1
= HoverTool(tooltips=[('Total Births','@total'),('Percentage of All Births','@percent%')])

hover2
= HoverTool(tooltips=[('Total Births','@total'),('Percentage of All Births','@percent%')])

p1
.add_tools(hover1)

p2
.add_tools(hover2)

p1
.xaxis[0].formatter = NumeralTickFormatter(format="0,")

p2
.xaxis[0].formatter = NumeralTickFormatter(format="0,")

def update_plots(attr, old, new):

    time
= slider.
value
    bools1
, bools2 = set_bools(time, 'county'), set_bools(time, 'state')

    view1
, view2 = set_views(bools1), set_views(bools2)

    c1
.view, c2.view = view1,
view2

slider
= Slider(start=1990, end=2007, step=1, value=1990, title='Year')

slider
.on_change('value', update_plots)

layout
= row(widgetbox(slider), p1, p2)

curdoc
().add_root(layout)

I suspect that the error may have to do with the views I've defined because the legend is correctly mapped when I remove them, but obviously the points are displayed incorrectly. When I view the plots, the glyph for 'black' is mapped correctly to the legend, but the other categories ('hispanic', 'white', 'other') appear as black dots in the legend although they appear correctly on the plot. Thanks in advance for the help!

--
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/5c9cccfe-a519-4fd0-a1e2-bff3f9544b14%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Hey Bryan,

Thanks for the reply. I do have a column for colors that I’ve mapped. When I pass the ‘color’ column to the color attribute of my glyph, the same problem arises. Were you suggesting something different? I may have misunderstood what you meant about using the ‘color’ column for the legend.

All the Best,

Dayo

···

On Saturday, April 14, 2018 at 11:15:03 AM UTC-5, Bryan Van de ven wrote:

Hi,

My suspicion is that the interaction of transforms with “groupby” legends has never been explicitly considered, and so probably does not do anything desirable. A GH issue to request making these two features work better would be appropriate. As a workaround in the mean time, I can only suggest colormapping the data yourself in python (instead of using factor_cmap) and sending the colors as their own column.

Thanks,

Bryan

On Apr 14, 2018, at 11:08, [email protected] wrote:

I’ve created a bokeh app that displays birth data at the county and state levels between 1990 and 2007. The issue is that the legend I’ve defined doesn’t display information for all of the factors in the specified column (‘race’). Here is my source code:

from bokeh.io import output_notebook, output_file, show,

curdoc

from bokeh.plotting import

figure

from bokeh.models import ColumnDataSource, HoverTool, Slider, CDSView, BooleanFilter, NumeralTickFormatter

from bokeh.palettes import Spectral as

palette

from bokeh.layouts import widgetbox, row,

column

from bokeh.transform import

factor_cmap

source
= ColumnDataSource(data=dict(final_merge.reset_index()))

races
= [‘black’, ‘hispanic’, ‘other’, ‘white’]

def set_bools(yr, lvl):

return [True if (year == yr) and (level == lvl) else False for year, level in zip(source.data[‘year’], source.data[‘level’])]

def set_views(boolean):

return CDSView(source=source, filters=[BooleanFilter(boolean)])

p1
= figure(x_axis_label = ‘Number of Births’, y_axis_label = ‘Percentage of Births’,

            title

=‘Harris County Births 1990-2007’, x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

p2
= figure(x_axis_label = ‘Number of Births’, y_axis_label = ‘Percentage of Births’,

            title

=‘Texas Births 1990-2007’, x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

bools1

, bools2 = set_bools(1990, ‘county’), set_bools(1990, ‘state’)

view1

, view2 = set_views(bools1), set_views(bools2)

c1
= p1.circle(x=‘total’, y=‘percent’, size=10, color=factor_cmap(‘race’, palette[4], races), legend=‘race’, source=source)

c2 = p2.circle(x=‘total’, y=‘percent’, size=10, color=factor_cmap(‘race’, palette[4], races), legend=‘race’, source=source)

c1

.view, c2.view = view1,

view2

p1

.legend.location = ‘bottom_right’

p2

.legend.location = ‘bottom_right’

hover1
= HoverTool(tooltips=[(‘Total Births’,‘@total’),(‘Percentage of All Births’,‘@percent%’)])

hover2
= HoverTool(tooltips=[(‘Total Births’,‘@total’),(‘Percentage of All Births’,‘@percent%’)])

p1

.add_tools(hover1)

p2

.add_tools(hover2)

p1

.xaxis[0].formatter = NumeralTickFormatter(format=“0,”)

p2

.xaxis[0].formatter = NumeralTickFormatter(format=“0,”)

def update_plots(attr, old, new):

time

= slider.

value

bools1

, bools2 = set_bools(time, ‘county’), set_bools(time, ‘state’)

view1

, view2 = set_views(bools1), set_views(bools2)

c1

.view, c2.view = view1,

view2

slider
= Slider(start=1990, end=2007, step=1, value=1990, title=‘Year’)

slider

.on_change(‘value’, update_plots)

layout
= row(widgetbox(slider), p1, p2)

curdoc

().add_root(layout)

I suspect that the error may have to do with the views I’ve defined because the legend is correctly mapped when I remove them, but obviously the points are displayed incorrectly. When I view the plots, the glyph for ‘black’ is mapped correctly to the legend, but the other categories (‘hispanic’, ‘white’, ‘other’) appear as black dots in the legend although they appear correctly on the plot. Thanks in advance for the help!


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/5c9cccfe-a519-4fd0-a1e2-bff3f9544b14%40continuum.io.

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

In the code you’ve posted you do not have colors colormspped in python, you have pass a factor_cmap which represents a colormapping done on the fly, in the browser. There’s never actually a column in the CDS with the colors (you could call it a computed column). This is what I suspect does not interact with groupby legends. Are your saying you have also tried literally constructing a concrete list of colors in python by hand, and put those in your data source, and that that also did not work?

Bryan

···

On Apr 14, 2018, at 11:51, Ẹkúndayọ̀ Àzụ́bụ̀íké [email protected] wrote:

Hey Bryan,

Thanks for the reply. I do have a column for colors that I’ve mapped. When I pass the ‘color’ column to the color attribute of my glyph, the same problem arises. Were you suggesting something different? I may have misunderstood what you meant about using the ‘color’ column for the legend.

All the Best,

Dayo

On Saturday, April 14, 2018 at 11:15:03 AM UTC-5, Bryan Van de ven wrote:

Hi,

My suspicion is that the interaction of transforms with “groupby” legends has never been explicitly considered, and so probably does not do anything desirable. A GH issue to request making these two features work better would be appropriate. As a workaround in the mean time, I can only suggest colormapping the data yourself in python (instead of using factor_cmap) and sending the colors as their own column.

Thanks,

Bryan

On Apr 14, 2018, at 11:08, [email protected] wrote:

I’ve created a bokeh app that displays birth data at the county and state levels between 1990 and 2007. The issue is that the legend I’ve defined doesn’t display information for all of the factors in the specified column (‘race’). Here is my source code:

from bokeh.io import output_notebook, output_file, show,

curdoc

from bokeh.plotting import

figure

from bokeh.models import ColumnDataSource, HoverTool, Slider, CDSView, BooleanFilter, NumeralTickFormatter

from bokeh.palettes import Spectral as

palette

from bokeh.layouts import widgetbox, row,

column

from bokeh.transform import

factor_cmap

source
= ColumnDataSource(data=dict(final_merge.reset_index()))

races
= [‘black’, ‘hispanic’, ‘other’, ‘white’]

def set_bools(yr, lvl):

return [True if (year == yr) and (level == lvl) else False for year, level in zip(source.data[‘year’], source.data[‘level’])]

def set_views(boolean):

return CDSView(source=source, filters=[BooleanFilter(boolean)])

p1
= figure(x_axis_label = ‘Number of Births’, y_axis_label = ‘Percentage of Births’,

            title

=‘Harris County Births 1990-2007’, x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

p2
= figure(x_axis_label = ‘Number of Births’, y_axis_label = ‘Percentage of Births’,

            title

=‘Texas Births 1990-2007’, x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

bools1

, bools2 = set_bools(1990, ‘county’), set_bools(1990, ‘state’)

view1

, view2 = set_views(bools1), set_views(bools2)

c1
= p1.circle(x=‘total’, y=‘percent’, size=10, color=factor_cmap(‘race’, palette[4], races), legend=‘race’, source=source)

c2 = p2.circle(x=‘total’, y=‘percent’, size=10, color=factor_cmap(‘race’, palette[4], races), legend=‘race’, source=source)

c1

.view, c2.view = view1,

view2

p1

.legend.location = ‘bottom_right’

p2

.legend.location = ‘bottom_right’

hover1
= HoverTool(tooltips=[(‘Total Births’,‘@total’),(‘Percentage of All Births’,‘@percent%’)])

hover2
= HoverTool(tooltips=[(‘Total Births’,‘@total’),(‘Percentage of All Births’,‘@percent%’)])

p1

.add_tools(hover1)

p2

.add_tools(hover2)

p1

.xaxis[0].formatter = NumeralTickFormatter(format=“0,”)

p2

.xaxis[0].formatter = NumeralTickFormatter(format=“0,”)

def update_plots(attr, old, new):

time

= slider.

value

bools1

, bools2 = set_bools(time, ‘county’), set_bools(time, ‘state’)

view1

, view2 = set_views(bools1), set_views(bools2)

c1

.view, c2.view = view1,

view2

slider
= Slider(start=1990, end=2007, step=1, value=1990, title=‘Year’)

slider

.on_change(‘value’, update_plots)

layout
= row(widgetbox(slider), p1, p2)

curdoc

().add_root(layout)

I suspect that the error may have to do with the views I’ve defined because the legend is correctly mapped when I remove them, but obviously the points are displayed incorrectly. When I view the plots, the glyph for ‘black’ is mapped correctly to the legend, but the other categories (‘hispanic’, ‘white’, ‘other’) appear as black dots in the legend although they appear correctly on the plot. Thanks in advance for the help!


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/5c9cccfe-a519-4fd0-a1e2-bff3f9544b14%40continuum.io.

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

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/da0f1dc5-1b5e-4a93-838b-09393d3110c5%40continuum.io.

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

Yes, although not visible in the source code I provided, I do in fact have a separate ‘color’ column in the CDS that I created in base Python. I tried setting the color attribute of the circle glyph to ‘color’ and the legend attribute to ‘race’, but the same issue arises.

···

On Saturday, April 14, 2018 at 1:53:21 PM UTC-5, Bryan Van de ven wrote:

In the code you’ve posted you do not have colors colormspped in python, you have pass a factor_cmap which represents a colormapping done on the fly, in the browser. There’s never actually a column in the CDS with the colors (you could call it a computed column). This is what I suspect does not interact with groupby legends. Are your saying you have also tried literally constructing a concrete list of colors in python by hand, and put those in your data source, and that that also did not work?

Bryan

On Apr 14, 2018, at 11:51, Ẹkúndayọ̀ Àzụ́bụ̀íké [email protected] wrote:

Hey Bryan,

Thanks for the reply. I do have a column for colors that I’ve mapped. When I pass the ‘color’ column to the color attribute of my glyph, the same problem arises. Were you suggesting something different? I may have misunderstood what you meant about using the ‘color’ column for the legend.

All the Best,

Dayo

On Saturday, April 14, 2018 at 11:15:03 AM UTC-5, Bryan Van de ven wrote:

Hi,

My suspicion is that the interaction of transforms with “groupby” legends has never been explicitly considered, and so probably does not do anything desirable. A GH issue to request making these two features work better would be appropriate. As a workaround in the mean time, I can only suggest colormapping the data yourself in python (instead of using factor_cmap) and sending the colors as their own column.

Thanks,

Bryan

On Apr 14, 2018, at 11:08, [email protected] wrote:

I’ve created a bokeh app that displays birth data at the county and state levels between 1990 and 2007. The issue is that the legend I’ve defined doesn’t display information for all of the factors in the specified column (‘race’). Here is my source code:

from bokeh.io import output_notebook, output_file, show,

curdoc

from bokeh.plotting import

figure

from bokeh.models import ColumnDataSource, HoverTool, Slider, CDSView, BooleanFilter, NumeralTickFormatter

from bokeh.palettes import Spectral as

palette

from bokeh.layouts import widgetbox, row,

column

from bokeh.transform import

factor_cmap

source
= ColumnDataSource(data=dict(final_merge.reset_index()))

races
= [‘black’, ‘hispanic’, ‘other’, ‘white’]

def set_bools(yr, lvl):

return [True if (year == yr) and (level == lvl) else False for year, level in zip(source.data[‘year’], source.data[‘level’])]

def set_views(boolean):

return CDSView(source=source, filters=[BooleanFilter(boolean)])

p1
= figure(x_axis_label = ‘Number of Births’, y_axis_label = ‘Percentage of Births’,

            title

=‘Harris County Births 1990-2007’, x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

p2
= figure(x_axis_label = ‘Number of Births’, y_axis_label = ‘Percentage of Births’,

            title

=‘Texas Births 1990-2007’, x_range=(0, x_max + 10000), y_range=(0, y_max + 5))

bools1

, bools2 = set_bools(1990, ‘county’), set_bools(1990, ‘state’)

view1

, view2 = set_views(bools1), set_views(bools2)

c1
= p1.circle(x=‘total’, y=‘percent’, size=10, color=factor_cmap(‘race’, palette[4], races), legend=‘race’, source=source)

c2 = p2.circle(x=‘total’, y=‘percent’, size=10, color=factor_cmap(‘race’, palette[4], races), legend=‘race’, source=source)

c1

.view, c2.view = view1,

view2

p1

.legend.location = ‘bottom_right’

p2

.legend.location = ‘bottom_right’

hover1
= HoverTool(tooltips=[(‘Total Births’,‘@total’),(‘Percentage of All Births’,‘@percent%’)])

hover2
= HoverTool(tooltips=[(‘Total Births’,‘@total’),(‘Percentage of All Births’,‘@percent%’)])

p1

.add_tools(hover1)

p2

.add_tools(hover2)

p1

.xaxis[0].formatter = NumeralTickFormatter(format=“0,”)

p2

.xaxis[0].formatter = NumeralTickFormatter(format=“0,”)

def update_plots(attr, old, new):

time

= slider.

value

bools1

, bools2 = set_bools(time, ‘county’), set_bools(time, ‘state’)

view1

, view2 = set_views(bools1), set_views(bools2)

c1

.view, c2.view = view1,

view2

slider
= Slider(start=1990, end=2007, step=1, value=1990, title=‘Year’)

slider

.on_change(‘value’, update_plots)

layout
= row(widgetbox(slider), p1, p2)

curdoc

().add_root(layout)

I suspect that the error may have to do with the views I’ve defined because the legend is correctly mapped when I remove them, but obviously the points are displayed incorrectly. When I view the plots, the glyph for ‘black’ is mapped correctly to the legend, but the other categories (‘hispanic’, ‘white’, ‘other’) appear as black dots in the legend although they appear correctly on the plot. Thanks in advance for the help!


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/5c9cccfe-a519-4fd0-a1e2-bff3f9544b14%40continuum.io.

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

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/da0f1dc5-1b5e-4a93-838b-09393d3110c5%40continuum.io.

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