Giving a different colour to each tick label

Dear all,

I am trying to give each y tick label a different colour in my plot below. For example, I want to show “Very Poor” in Red, “Poor” in Orange, “Fair” in Yellow, “Good” in Green and “Very Good” in Blue. Is there a way to do this with Bokeh?

I can set all y tick labels to one colour but not to different colours. As a workaround, I tried having 5 y axes, each with one label, so I can set colour of them individually. But it seems I cannot control axis position to make them overlap.

This is an example of what I am trying to achieve for y axis tick labels.

Untitled.png

And this a toy problem (code) if you would like to give it a go.

from bokeh.plotting import output_file, show,figure
from bokeh.models.sources import ColumnDataSource
from bokeh.transform import linear_cmap
from bokeh.models import Range1d, FuncTickFormatter

import json
import pandas as pd

output_file(“fancylabels.html”)

#Get the data
score=[4.5, 9.32, 3.4, 7.1,1.4]

df = pd.DataFrame(score, columns = [‘Score’])
df.index = pd.to_datetime([‘2000-12-30 22:00:00’,‘2001-12-30 22:00:00’,‘2002-12-30 22:00:00’,‘2003-12-30 22:00:00’,‘2004-12-30 22:00:00’])

df.index.name = ‘Date’
df.sort_index(inplace=True)
source = ColumnDataSource(df)

#Prepare the plot area
p = figure(x_axis_type=“datetime”, plot_width=800, plot_height=500)
p.y_range = Range1d(0, 10)
def custom_label():
new_labels = [“Very Poor”, “Poor”, “Fair”, “Good”, “Very Good”]
return new_labels[(tick-1)/2 ]

p.yaxis.ticker = [1, 3, 5,7,9]
p.yaxis.formatter = FuncTickFormatter.from_py_func(custom_label)

mapper = linear_cmap(field_name=‘Score’, palette=[“red”, “orange”,“yellow”,“green”,“blue”] ,low=0 ,high=10)

p.circle(‘Date’, ‘Score’, source=source, line_width=5, line_color=mapper,color=mapper)
p.line(‘Date’, ‘Score’, source=source, line_width=2, line_color=“gray”,color=mapper, line_alpha = 0.5)

``

Thank you,

Bhagya

Hi,

This is not currently possible with any built in functionality. You could potentially create a custom extension to do this, but Axes are one of the most complicated code in BokehJS, so it would not be completely trivial to accomplish this.

Offhand, the only easier workaround I can suggest is to use Labels (which can be independently styled) in some way to simulate axis labels:

  https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#labels

Thanks,

Bryan

···

On Aug 15, 2018, at 23:03, [email protected] wrote:

Dear all,

I am trying to give each y tick label a different colour in my plot below. For example, I want to show "Very Poor" in Red, "Poor" in Orange, "Fair" in Yellow, "Good" in Green and "Very Good" in Blue. Is there a way to do this with Bokeh?

I can set all y tick labels to one colour but not to different colours. As a workaround, I tried having 5 y axes, each with one label, so I can set colour of them individually. But it seems I cannot control axis position to make them overlap.

This is an example of what I am trying to achieve for y axis tick labels.

<Untitled.png>

And this a toy problem (code) if you would like to give it a go.

from bokeh.plotting import output_file, show,figure
from bokeh.models.sources import ColumnDataSource
from bokeh.transform import linear_cmap
from bokeh.models import Range1d, FuncTickFormatter

import json
import pandas as pd

output_file("fancylabels.html")

#Get the data
score=[4.5, 9.32, 3.4, 7.1,1.4]

df = pd.DataFrame(score, columns = ['Score'])
df.index = pd.to_datetime(['2000-12-30 22:00:00','2001-12-30 22:00:00','2002-12-30 22:00:00','2003-12-30 22:00:00','2004-12-30 22:00:00'])

df.index.name = 'Date'
df.sort_index(inplace=True)
source = ColumnDataSource(df)

#Prepare the plot area
p = figure(x_axis_type="datetime", plot_width=800, plot_height=500)
p.y_range = Range1d(0, 10)
def custom_label():
new_labels = ["Very Poor", "Poor", "Fair", "Good", "Very Good"]
return new_labels[(tick-1)/2 ]

p.yaxis.ticker = [1, 3, 5,7,9]
p.yaxis.formatter = FuncTickFormatter.from_py_func(custom_label)

mapper = linear_cmap(field_name='Score', palette=["red", "orange","yellow","green","blue"] ,low=0 ,high=10)

p.circle('Date', 'Score', source=source, line_width=5, line_color=mapper,color=mapper)
p.line('Date', 'Score', source=source, line_width=2, line_color="gray",color=mapper, line_alpha = 0.5)

Thank you,
Bhagya

--
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/74a97048-50e8-4db1-8998-abc2094d0fbc%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
<Untitled.png>

Thanks, that actually worked. I set the alpha of original y-tick labels to 0 and moved each custom Label to the required positions.

Cheers,

Bhagya

···

On Thu, 16 Aug 2018 at 16:10, Bryan Van de ven [email protected] wrote:

Hi,

This is not currently possible with any built in functionality. You could potentially create a custom extension to do this, but Axes are one of the most complicated code in BokehJS, so it would not be completely trivial to accomplish this.

Offhand, the only easier workaround I can suggest is to use Labels (which can be independently styled) in some way to simulate axis labels:

    [https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#labels](https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#labels)

Thanks,

Bryan

On Aug 15, 2018, at 23:03, [email protected] wrote:

Dear all,

I am trying to give each y tick label a different colour in my plot below. For example, I want to show “Very Poor” in Red, “Poor” in Orange, “Fair” in Yellow, “Good” in Green and “Very Good” in Blue. Is there a way to do this with Bokeh?

I can set all y tick labels to one colour but not to different colours. As a workaround, I tried having 5 y axes, each with one label, so I can set colour of them individually. But it seems I cannot control axis position to make them overlap.

This is an example of what I am trying to achieve for y axis tick labels.

<Untitled.png>

And this a toy problem (code) if you would like to give it a go.

from bokeh.plotting import output_file, show,figure

from bokeh.models.sources import ColumnDataSource

from bokeh.transform import linear_cmap

from bokeh.models import Range1d, FuncTickFormatter

import json

import pandas as pd

output_file(“fancylabels.html”)

#Get the data

score=[4.5, 9.32, 3.4, 7.1,1.4]

df = pd.DataFrame(score, columns = [‘Score’])

df.index = pd.to_datetime([‘2000-12-30 22:00:00’,‘2001-12-30 22:00:00’,‘2002-12-30 22:00:00’,‘2003-12-30 22:00:00’,‘2004-12-30 22:00:00’])

df.index.name = ‘Date’

df.sort_index(inplace=True)

source = ColumnDataSource(df)

#Prepare the plot area

p = figure(x_axis_type=“datetime”, plot_width=800, plot_height=500)

p.y_range = Range1d(0, 10)

def custom_label():

new_labels = [“Very Poor”, “Poor”, “Fair”, “Good”, “Very Good”]

return new_labels[(tick-1)/2 ]

p.yaxis.ticker = [1, 3, 5,7,9]

p.yaxis.formatter = FuncTickFormatter.from_py_func(custom_label)

mapper = linear_cmap(field_name=‘Score’, palette=[“red”, “orange”,“yellow”,“green”,“blue”] ,low=0 ,high=10)

p.circle(‘Date’, ‘Score’, source=source, line_width=5, line_color=mapper,color=mapper)

p.line(‘Date’, ‘Score’, source=source, line_width=2, line_color=“gray”,color=mapper, line_alpha = 0.5)

Thank you,

Bhagya

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/74a97048-50e8-4db1-8998-abc2094d0fbc%40continuum.io.

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

<Untitled.png>

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

To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/bokeh/Y2qG1qt2y4c/unsubscribe.

To unsubscribe from this group and all its topics, 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/01977768-1E1D-4037-A7F8-C6CA3941F673%40anaconda.com.

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