how to add $ and other formatting to data labels rendered using labelset?

I am trying to format the text for data labels to show contribution amounts in $. Not sure how to do this. Read the documentation but couldnt find much help. Attached is the sample of the contribution data. I am highlighted what I was trying to do but obviously it is throwing an error. Any suggestions?

import pandas as pd
from bokeh.models import ColumnDataSource, LinearColorMapper, LabelSet, PrintfTickFormatter
from bokeh.plotting import figure, show, output_notebook

camp = pd.read_excel(’…/data/sampleContributions.xlsx’)

campDF= camp.groupby(‘OFFICECD’)[‘AMNT’].sum()
campCDS = ColumnDataSource(data=dict(xvar=campDF.index, yval=campDF.values))

p = figure(x_range=list(campDF.index), width=800, height=500)
p.vbar(‘xvar’, top=‘yval’, width=.5, source=campCDS)

barLabels = LabelSet(x=‘xvar’, y=‘yval’, text=’{:.2f}’.format(‘yval’), source=campCDS )
p.add_layout(barLabels)

output_notebook()
show§

``

sampleContributions.xlsx (37.1 KB)

Hi,

There are two approaches. Either way, you will want to put the data into a column in campCDS:

* preformat the values into strings (in Python), and put these values as column in campCDS. Then the LabelSet should be configured with

  text="my_labels_colname"

Or:

* put the raw numeric data in campCDS and use a CustomJSTransform to format the numbers to strings (with $ in front or whatever) in the browser.

The first method is probably what I'd suggest, absent any other information.

Thanks,

Bryan

···

On Dec 11, 2017, at 16:38, RJ <[email protected]> wrote:

I am trying to format the text for data labels to show contribution amounts in $. Not sure how to do this. Read the documentation but couldnt find much help. Attached is the sample of the contribution data. I am highlighted what I was trying to do but obviously it is throwing an error. Any suggestions?

import pandas as pd
from bokeh.models import ColumnDataSource, LinearColorMapper, LabelSet, PrintfTickFormatter
from bokeh.plotting import figure, show, output_notebook

camp = pd.read_excel('../data/sampleContributions.xlsx')

campDF= camp.groupby('OFFICECD')['AMNT'].sum()
campCDS = ColumnDataSource(data=dict(xvar=campDF.index, yval=campDF.values))

p = figure(x_range=list(campDF.index), width=800, height=500)
p.vbar('xvar', top='yval', width=.5, source=campCDS)

barLabels = LabelSet(x='xvar', y='yval', text='{:.2f}'.format('yval'), source=campCDS )
p.add_layout(barLabels)

output_notebook()
show(p)

--
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/3fbf050c-b54c-455d-aa46-236bf29e6380%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
<sampleContributions.xlsx>

Thanks! The first approach worked. But I was hoping there would have been simpler way to just format the display of text…

···

On Thursday, December 14, 2017 at 1:14:05 PM UTC-5, Bryan Van de ven wrote:

Hi,

There are two approaches. Either way, you will want to put the data into a column in campCDS:

  • preformat the values into strings (in Python), and put these values as column in campCDS. Then the LabelSet should be configured with

      text="my_labels_colname"
    

Or:

  • put the raw numeric data in campCDS and use a CustomJSTransform to format the numbers to strings (with $ in front or whatever) in the browser.

The first method is probably what I’d suggest, absent any other information.

Thanks,

Bryan

On Dec 11, 2017, at 16:38, RJ [email protected] wrote:

I am trying to format the text for data labels to show contribution amounts in $. Not sure how to do this. Read the documentation but couldnt find much help. Attached is the sample of the contribution data. I am highlighted what I was trying to do but obviously it is throwing an error. Any suggestions?

import pandas as pd

from bokeh.models import ColumnDataSource, LinearColorMapper, LabelSet, PrintfTickFormatter

from bokeh.plotting import figure, show, output_notebook

camp = pd.read_excel(’…/data/sampleContributions.xlsx’)

campDF= camp.groupby(‘OFFICECD’)[‘AMNT’].sum()

campCDS = ColumnDataSource(data=dict(xvar=campDF.index, yval=campDF.values))

p = figure(x_range=list(campDF.index), width=800, height=500)

p.vbar(‘xvar’, top=‘yval’, width=.5, source=campCDS)

barLabels = LabelSet(x=‘xvar’, y=‘yval’, text=’{:.2f}’.format(‘yval’), source=campCDS )

p.add_layout(barLabels)

output_notebook()

show§


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/3fbf050c-b54c-455d-aa46-236bf29e6380%40continuum.io.

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

<sampleContributions.xlsx>

Hi, there are formatters (e.g. PrintfFormatter and others) that can do that simply for HoverTool tooltips. It's possible it would make sense to make them available to labels some how as well, but that would be new development (so a GH issue to propose it would be the next step)

Thanks,

Bryan

···

On Dec 18, 2017, at 14:27, [email protected] wrote:

Thanks! The first approach worked. But I was hoping there would have been simpler way to just format the display of text...

On Thursday, December 14, 2017 at 1:14:05 PM UTC-5, Bryan Van de ven wrote:
Hi,

There are two approaches. Either way, you will want to put the data into a column in campCDS:

* preformat the values into strings (in Python), and put these values as column in campCDS. Then the LabelSet should be configured with

        text="my_labels_colname"

Or:

* put the raw numeric data in campCDS and use a CustomJSTransform to format the numbers to strings (with $ in front or whatever) in the browser.

The first method is probably what I'd suggest, absent any other information.

Thanks,

Bryan

> On Dec 11, 2017, at 16:38, RJ <[email protected]> wrote:
>
>
> I am trying to format the text for data labels to show contribution amounts in $. Not sure how to do this. Read the documentation but couldnt find much help. Attached is the sample of the contribution data. I am highlighted what I was trying to do but obviously it is throwing an error. Any suggestions?
>
> import pandas as pd
> from bokeh.models import ColumnDataSource, LinearColorMapper, LabelSet, PrintfTickFormatter
> from bokeh.plotting import figure, show, output_notebook
>
> camp = pd.read_excel('../data/sampleContributions.xlsx')
>
> campDF= camp.groupby('OFFICECD')['AMNT'].sum()
> campCDS = ColumnDataSource(data=dict(xvar=campDF.index, yval=campDF.values))
>
> p = figure(x_range=list(campDF.index), width=800, height=500)
> p.vbar('xvar', top='yval', width=.5, source=campCDS)
>
> barLabels = LabelSet(x='xvar', y='yval', text='{:.2f}'.format('yval'), source=campCDS )
> p.add_layout(barLabels)
>
>
> output_notebook()
> show(p)
>
>
> --
> 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/3fbf050c-b54c-455d-aa46-236bf29e6380%40continuum.io.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout.
> <sampleContributions.xlsx>

--
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/1bd15880-c5d1-47ee-bc03-61a8305dd930%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.