# How color mapper works ?

**URL:** <https://discourse.bokeh.org/t/how-color-mapper-works/2171>\
**Category:** Community Support\
**Created:** [May 4, 2017, 7:43am UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171 "2017-05-04T07:43:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Schmitt](https://discourse.bokeh.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Alex\_Schmitt](https://discourse.bokeh.org/u/Alex_Schmitt)\
**Post date:** [May 4, 2017, 7:43am UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/1 "2017-05-04T07:43:41Z")

</div>

Hi,

I tried to make a basic correlation matrix with a linear color bar on the side, and got a Value Error with the dictionnary mapping values to LinearColorMaper. I don’t understand why. Any body could light me up ?

from bokeh.plotting import figure, show, output\_notebook

from bokeh.palettes import Viridis256

from bokeh.models import ColumnDataSource, ColorBar, LinearColorMapper

from bokeh.sampledata.autompg import autompg

corr = autompg.corr()

cols = list( corr.columns)

x = y = c =

for i in cols:

for j in cols:

x.append( j)

y.append( i)

c.append( abs( corr[i][j]))

mapper = LinearColorMapper( palette=Viridis256, low=0, high=1)

colors= { ‘field’: c, ‘transform’: mapper}

color\_bar = ColorBar( color\_mapper=mapper, location=( 0, 0))

p = figure( toolbar\_location=None, tools=‘’, x\_range=cols, y\_range=cols)

p.rect( x, y, fill\_color=colors, line\_color=‘black’, width=1, height=1)

p.add\_layout( color\_bar, ‘right’)

output\_notebook()

show(p)

# =\> ValueError: expected an element of either … # on p.rect( …, color=colors, …

Thx

alEx

---

<div class="post-metadata">

**Author:** ![Raphael\_Walker1](https://discourse.bokeh.org/letter_avatar_proxy/v4/letter/r/e79b87/32.png) [@Raphael\_Walker1](https://discourse.bokeh.org/u/Raphael_Walker1)\
**Post date:** [May 4, 2017, 8:18am UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/2 "2017-05-04T08:18:00Z")

</div>

Hi,

```
  Try ColumnDataSource. Code below doesn't throw an error but the

```

plot is not showing anything. Perhaps this helps for the next  
step.

```auto

      from bokeh.plotting import figure, show, output_notebook,
output_file
      from bokeh.palettes import Viridis256
      from bokeh.models import ColumnDataSource, ColorBar,
LinearColorMapper
      from bokeh.sampledata.autompg import autompg

      corr = autompg.corr()
      cols = list( corr.columns)

      x = y = c = []

      for i in cols:
          for j in cols:
              x.append( j)
              y.append( i)
              c.append( abs( corr[i][j]))

      mapper = LinearColorMapper( palette=Viridis256, low=0, high=1)
      colors= { 'field': c, 'transform': mapper}
      color_bar = ColorBar( color_mapper=mapper, location=( 0, 0))

      p = figure( toolbar_location=None, tools='', x_range=cols,
y_range=cols)
      source = ColumnDataSource(dict(x=x, y=y, color=c))
      p.rect( 'x', 'y', source=source, fill_color={'field': 'color',
'transform': mapper}, line_color='black', width=1, height=1)
      p = figure( toolbar_location=None, tools='', x_range=cols,
y_range=cols)
      p.add_layout( color_bar, 'right')

      #output_file(r'/tmp/test.html')
      output_notebook()

      show(p)

```

```
Have a nice day,

Raphael

```

> **···**
>
> On 2017-05-04 09:43, alEx S wrote:
> 
> > Hi,
> 
> > ```
> > I tried to make a basic correlation matrix with a linear
> > 
> > ```
> > 
> > color bar on the side, and got a Value Error with the  
> > dictionnary mapping values to LinearColorMaper. I don’t  
> > understand why. Any body could light me up ?
> 
> > 
> 
> > from bokeh.plotting import figure, show, output\_notebook
> 
> > from bokeh.palettes import Viridis256
> 
> > ```
> > from bokeh.models import ColumnDataSource, ColorBar,
> > 
> > ```
> > 
> > LinearColorMapper
> 
> > from bokeh.sampledata.autompg import autompg
> 
> > 
> 
> > corr = autompg.corr()
> 
> > cols = list( corr.columns)
> 
> > 
> 
> > x = y = c =
> 
> > 
> 
> > for i in cols:
> 
> > for j in cols:
> 
> > x.append( j)
> 
> > y.append( i)
> 
> > c.append( abs( corr[i][j]))
> 
> > 
> 
> > ```
> > mapper = LinearColorMapper( palette=Viridis256, low=0,
> > 
> > ```
> > 
> > high=1)
> 
> > colors= { ‘field’: c, ‘transform’: mapper}
> 
> > ```
> > color_bar = ColorBar( color_mapper=mapper, location=( 0,
> > 
> > ```
> > 
> > 0))
> 
> > 
> 
> > ```
> > p = figure( toolbar_location=None, tools='',
> > 
> > ```
> > 
> > x\_range=cols, y\_range=cols)
> 
> > ```
> > p.rect( x, y, fill_color=colors, line_color='black',
> > 
> > ```
> > 
> > width=1, height=1)
> 
> > p.add\_layout( color\_bar, ‘right’)
> 
> > 
> 
> > output\_notebook()
> 
> > show(p)
> 
> > 
> 
> > # =\> ValueError: expected an element of either … # on p.rect( …, color=colors, …
> 
> > 
> 
> > Thx
> 
> > alEx
> 
> > –
> > 
> > ```
> > 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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > 
> > ```
> > To post to this group, send email to bokeh@continuum.io.
> > 
> > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io](https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io?utm_medium=email&utm_source=footer).
> > 
> > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout).
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Alex\_Schmitt](https://discourse.bokeh.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Alex\_Schmitt](https://discourse.bokeh.org/u/Alex_Schmitt)\
**Post date:** [May 4, 2017, 8:48am UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/3 "2017-05-04T08:48:23Z")

</div>

Thx Raphael.

The thing is I’m new to python and I don’t know where to look for how mapper works. If I try to just map or apply the mapper to the correlation values I have: TypeError: ‘LinearColorMapper’ object is not callable

import pandas as pd

source = pd.DataFrame( dict(x=x, y=y, color=c))  
source[‘color’].map( mapper)

source[‘color’].apply( lambda x: mapper( x))

I really don’t know what to do !

All the best

alEx

> **···**
>
> On Thu, May 4, 2017 at 10:18 AM Raphael Walker [web@busino.ch](mailto:web@busino.ch) wrote:
> 
> > Hi,
> 
> > ```
> > Try ColumnDataSource. Code below doesn't throw an error but the
> > 
> > ```
> > 
> > plot is not showing anything. Perhaps this helps for the next  
> > step.
> 
> > ```auto
> > 
> > ```
> 
> > ```
> > from bokeh.plotting import figure, show, output_notebook,
> > 
> > ```
> > 
> > output\_file
> 
> > ```
> > from bokeh.palettes import Viridis256
> > from bokeh.models import ColumnDataSource, ColorBar,
> > 
> > ```
> > 
> > LinearColorMapper  
> > from bokeh.sampledata.autompg import autompg
> > 
> > ```
> > corr = autompg.corr()
> > cols = list( corr.columns)
> > 
> > x = y = c = []
> > 
> > for i in cols:
> > for j in cols:
> > x.append( j)
> > y.append( i)
> > c.append( abs( corr[i][j]))
> > 
> > mapper = LinearColorMapper( palette=Viridis256, low=0, high=1)
> > colors= { 'field': c, 'transform': mapper}
> > color_bar = ColorBar( color_mapper=mapper, location=( 0, 0))
> > 
> > p = figure( toolbar_location=None, tools='', x_range=cols,
> > 
> > ```
> > 
> > y\_range=cols)
> 
> > source = ColumnDataSource(dict(x=x, y=y, color=c))  
> > p.rect( ‘x’, ‘y’, source=source, fill\_color={‘field’: ‘color’,  
> > ‘transform’: mapper}, line\_color=‘black’, width=1, height=1)
> 
> > ```
> > p = figure( toolbar_location=None, tools='', x_range=cols,
> > 
> > ```
> > 
> > y\_range=cols)
> 
> > p.add\_layout( color\_bar, ‘right’)
> > 
> > ```
> > #output_file(r'/tmp/test.html')
> > output_notebook()
> > 
> > show(p)
> > 
> > ```
> 
> > ```auto
> > 
> > Have a nice day,
> > 
> > Raphael
> > 
> > ```
> 
> > On 2017-05-04 09:43, alEx S wrote:
> 
> > > Hi,
> 
> > > ```
> > > I tried to make a basic correlation matrix with a linear
> > > 
> > > ```
> > > 
> > > color bar on the side, and got a Value Error with the  
> > > dictionnary mapping values to LinearColorMaper. I don’t  
> > > understand why. Any body could light me up ?
> 
> > >
> 
> > > from bokeh.plotting import figure, show, output\_notebook
> 
> > > from bokeh.palettes import Viridis256
> 
> > > ```
> > > from bokeh.models import ColumnDataSource, ColorBar,
> > > 
> > > ```
> > > 
> > > LinearColorMapper
> 
> > > from bokeh.sampledata.autompg import autompg
> 
> > >
> 
> > > corr = autompg.corr()
> 
> > > cols = list( corr.columns)
> 
> > >
> 
> > > x = y = c =
> 
> > >
> 
> > > for i in cols:
> 
> > > for j in cols:
> 
> > > x.append( j)
> 
> > > y.append( i)
> 
> > > c.append( abs( corr[i][j]))
> 
> > >
> 
> > > ```
> > > mapper = LinearColorMapper( palette=Viridis256, low=0,
> > > 
> > > ```
> > > 
> > > high=1)
> 
> > > colors= { ‘field’: c, ‘transform’: mapper}
> 
> > > ```
> > > color_bar = ColorBar( color_mapper=mapper, location=( 0,
> > > 
> > > ```
> > > 
> > > 0))
> 
> > >
> 
> > > ```
> > > p = figure( toolbar_location=None, tools='',
> > > 
> > > ```
> > > 
> > > x\_range=cols, y\_range=cols)
> 
> > > ```
> > > p.rect( x, y, fill_color=colors, line_color='black',
> > > 
> > > ```
> > > 
> > > width=1, height=1)
> 
> > > p.add\_layout( color\_bar, ‘right’)
> 
> > >
> 
> > > output\_notebook()
> 
> > > show(p)
> 
> > >
> 
> > > # =\> ValueError: expected an element of either … # on p.rect( …, color=colors, …
> 
> > >
> 
> > > Thx
> 
> > > alEx
> 
> > > –
> > > 
> > > ```
> > > 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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > > 
> > > ```
> > > To post to this group, send email to bokeh@continuum.io.
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io](https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io?utm_medium=email&utm_source=footer).
> > > 
> > > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > 
> > To post to this group, send email to [bokeh@continuum.io](mailto:bokeh@continuum.io).
> > 
> > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/7dd2e92b-1621-8f14-95b6-4042ec5e69f7%40busino.ch](https://groups.google.com/a/continuum.io/d/msgid/bokeh/7dd2e92b-1621-8f14-95b6-4042ec5e69f7%40busino.ch?utm_medium=email&utm_source=footer).
> > 
> > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout).

---

<div class="post-metadata">

**Author:** ![Raphael\_Walker1](https://discourse.bokeh.org/letter_avatar_proxy/v4/letter/r/e79b87/32.png) [@Raphael\_Walker1](https://discourse.bokeh.org/u/Raphael_Walker1)\
**Post date:** [May 4, 2017, 8:50am UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/4 "2017-05-04T08:50:48Z")

</div>

Hi Alex,

Please provide complete/runnable example.

```
  I don't see how you initialize the `mapper` in the code

```

snippet!!!

Raphael

> **···**
>
> On 2017-05-04 10:48, Alex Schmitt  
> wrote:
> 
> > Thx Raphael.
> 
> > ```
> > The thing is I'm new to python and I don't know where to
> > 
> > ```
> > 
> > look for how mapper works. If I try to just map or apply the  
> > mapper to the correlation values I have: TypeError: ‘LinearColorMapper’ object is not callable
> 
> > 
> 
> > import pandas as pd
> 
> > source = pd.DataFrame( dict(x=x, y=y, color=c))  
> > source[‘color’].map( mapper)
> 
> > source[‘color’].apply( lambda x: mapper( x))
> 
> > 
> 
> > I really don’t know what to do !
> 
> > 
> 
> > All the best
> 
> > alEx
> 
> > ```
> > On Thu, May 4, 2017 at 10:18 AM Raphael Walker
> > 
> > ```
> > 
> > \<web@busino.ch \>  
> > wrote:
> 
> > > Hi,
> 
> > > ```
> > > Try ColumnDataSource. Code below doesn't throw an error
> > > 
> > > ```
> > > 
> > > but the plot is not showing anything. Perhaps this helps  
> > > for the next step.
> 
> > > ```auto
> > > 
> > > ```
> 
> > > ```
> > > from bokeh.plotting import figure, show, output_notebook,
> > > 
> > > ```
> > > 
> > > output\_file
> 
> > > ```
> > > from bokeh.palettes import Viridis256
> > > from bokeh.models import ColumnDataSource, ColorBar,
> > > 
> > > ```
> > > 
> > > LinearColorMapper  
> > > from bokeh.sampledata.autompg import autompg
> > > 
> > > ```
> > > corr = autompg.corr()
> > > cols = list( corr.columns)
> > > 
> > > x = y = c = []
> > > 
> > > for i in cols:
> > > for j in cols:
> > > x.append( j)
> > > y.append( i)
> > > c.append( abs( corr[i][j]))
> > > 
> > > mapper = LinearColorMapper( palette=Viridis256, low=0,
> > > 
> > > ```
> > > 
> > > high=1)  
> > > colors= { ‘field’: c, ‘transform’: mapper}  
> > > color\_bar = ColorBar( color\_mapper=mapper, location=( 0,  
> > > 0))
> > > 
> > > ```
> > > p = figure( toolbar_location=None, tools='', x_range=cols,
> > > 
> > > ```
> > > 
> > > y\_range=cols)
> 
> > > source = ColumnDataSource(dict(x=x, y=y, color=c))  
> > > p.rect( ‘x’, ‘y’, source=source, fill\_color={‘field’:  
> > > ‘color’, ‘transform’: mapper}, line\_color=‘black’,  
> > > width=1, height=1)
> 
> > > ```
> > > p = figure( toolbar_location=None, tools='', x_range=cols,
> > > 
> > > ```
> > > 
> > > y\_range=cols)
> 
> > > p.add\_layout( color\_bar, ‘right’)
> > > 
> > > ```
> > > #output_file(r'/tmp/test.html')
> > > output_notebook()
> > > 
> > > show(p)
> > > 
> > > ```
> 
> > > ```auto
> > > 
> > > Have a nice day,
> > > 
> > > Raphael
> > > 
> > > ```
> 
> > > ```
> > > On
> > > 
> > > ```
> > > 
> > > 2017-05-04 09:43, alEx S wrote:
> 
> > > > Hi,
> 
> > > > ```
> > > > I tried to make a basic correlation matrix with a
> > > > 
> > > > ```
> > > > 
> > > > linear color bar on the side, and got a Value Error  
> > > > with the dictionnary mapping values to  
> > > > LinearColorMaper. I don’t understand why. Any body  
> > > > could light me up ?
> 
> > > >
> 
> > > > ```
> > > > from bokeh.plotting import figure, show,
> > > > 
> > > > ```
> > > > 
> > > > output\_notebook
> 
> > > > from bokeh.palettes import Viridis256
> 
> > > > ```
> > > > from bokeh.models import ColumnDataSource,
> > > > 
> > > > ```
> > > > 
> > > > ColorBar, LinearColorMapper
> 
> > > > from bokeh.sampledata.autompg import autompg
> 
> > > >
> 
> > > > corr = autompg.corr()
> 
> > > > cols = list( corr.columns)
> 
> > > >
> 
> > > > x = y = c =
> 
> > > >
> 
> > > > for i in cols:
> 
> > > > for j in cols:
> 
> > > > x.append( j)
> 
> > > > y.append( i)
> 
> > > > c.append( abs( corr[i][j]))
> 
> > > >
> 
> > > > ```
> > > > mapper = LinearColorMapper( palette=Viridis256,
> > > > 
> > > > ```
> > > > 
> > > > low=0, high=1)
> 
> > > > colors= { ‘field’: c, ‘transform’: mapper}
> 
> > > > ```
> > > > color_bar = ColorBar( color_mapper=mapper,
> > > > 
> > > > ```
> > > > 
> > > > location=( 0, 0))
> 
> > > >
> 
> > > > ```
> > > > p = figure( toolbar_location=None, tools='',
> > > > 
> > > > ```
> > > > 
> > > > x\_range=cols, y\_range=cols)
> 
> > > > ```
> > > > p.rect( x, y, fill_color=colors,
> > > > 
> > > > ```
> > > > 
> > > > line\_color=‘black’, width=1, height=1)
> 
> > > > p.add\_layout( color\_bar, ‘right’)
> 
> > > >
> 
> > > > output\_notebook()
> 
> > > > show(p)
> 
> > > >
> 
> > > > # =\> ValueError: expected an element of either … # on p.rect( …, color=colors, …
> 
> > > >
> 
> > > > Thx
> 
> > > > alEx
> 
> > > > –
> > > > 
> > > > ```
> > > > 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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > > > 
> > > > ```
> > > > To post to this group, send email to bokeh@continuum.io.
> > > > 
> > > > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io](https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io?utm_medium=email&utm_source=footer).
> > > > 
> > > > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > > 
> > > ```
> > > To post to this group, send email to bokeh@continuum.io.
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/7dd2e92b-1621-8f14-95b6-4042ec5e69f7%40busino.ch](https://groups.google.com/a/continuum.io/d/msgid/bokeh/7dd2e92b-1621-8f14-95b6-4042ec5e69f7%40busino.ch?utm_medium=email&utm_source=footer).
> > > 
> > > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > 
> > ```
> > To post to this group, send email to bokeh@continuum.io.
> > 
> > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/CAJjPzf_6PiV65zhK8uFgxPq%3DnZaQVanmBaR9-torQxrbU4koHw%40mail.gmail.com](https://groups.google.com/a/continuum.io/d/msgid/bokeh/CAJjPzf_6PiV65zhK8uFgxPq%3DnZaQVanmBaR9-torQxrbU4koHw%40mail.gmail.com?utm_medium=email&utm_source=footer).
> > 
> > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout).
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Raphael\_Walker1](https://discourse.bokeh.org/letter_avatar_proxy/v4/letter/r/e79b87/32.png) [@Raphael\_Walker1](https://discourse.bokeh.org/u/Raphael_Walker1)\
**Post date:** [May 4, 2017, 9:09am UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/5 "2017-05-04T09:09:29Z")

</div>

I think you should have a look at the working example:

Raphael

> **···**
>
> [https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color\_data\_map.py](https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py)  
> On 2017-05-04 10:50, Raphael Walker  
> wrote:
> 
> > Hi Alex,
> 
> > Please provide complete/runnable example.
> 
> > ```
> > I don't see how you initialize the `mapper` in the code
> > 
> > ```
> > 
> > snippet!!!
> 
> > Raphael
> 
> > ```
> > -- 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 .  
> > To post to this group, send email to .  
> > To view this discussion on the web visit .  
> > For more options, visit .
> 
> ```
> On 2017-05-04 10:48, Alex Schmitt
> 
> ```
> 
> wrote:
> 
> > Thx Raphael.
> 
> > ```
> > The thing is I'm new to python and I don't know where to
> > 
> > ```
> > 
> > look for how mapper works. If I try to just map or apply the  
> > mapper to the correlation values I have: TypeError: ‘LinearColorMapper’ object is not callable
> 
> > 
> 
> > import pandas as pd
> 
> > source = pd.DataFrame( dict(x=x, y=y, color=c))  
> > source[‘color’].map( mapper)
> 
> > source[‘color’].apply( lambda x: mapper( x))
> 
> > 
> 
> > I really don’t know what to do !
> 
> > 
> 
> > All the best
> 
> > alEx
> 
> > ```
> > On Thu, May 4, 2017 at 10:18 AM Raphael Walker
> > 
> > ```
> > 
> > \<web@busino.ch \>  
> > wrote:
> 
> > > Hi,
> 
> > > ```
> > > Try ColumnDataSource. Code below doesn't throw an error
> > > 
> > > ```
> > > 
> > > but the plot is not showing anything. Perhaps this helps  
> > > for the next step.
> 
> > > ```auto
> > > 
> > > ```
> 
> > > ```
> > > from bokeh.plotting import figure, show,
> > > 
> > > ```
> > > 
> > > output\_notebook, output\_file
> 
> > > ```
> > > from bokeh.palettes import Viridis256
> > > from bokeh.models import ColumnDataSource, ColorBar,
> > > 
> > > ```
> > > 
> > > LinearColorMapper  
> > > from bokeh.sampledata.autompg import autompg
> > > 
> > > ```
> > > corr = autompg.corr()
> > > cols = list( corr.columns)
> > > 
> > > x = y = c = []
> > > 
> > > for i in cols:
> > > for j in cols:
> > > x.append( j)
> > > y.append( i)
> > > c.append( abs( corr[i][j]))
> > > 
> > > mapper = LinearColorMapper( palette=Viridis256, low=0,
> > > 
> > > ```
> > > 
> > > high=1)  
> > > colors= { ‘field’: c, ‘transform’: mapper}  
> > > color\_bar = ColorBar( color\_mapper=mapper, location=( 0,  
> > > 0))
> > > 
> > > ```
> > > p = figure( toolbar_location=None, tools='',
> > > 
> > > ```
> > > 
> > > x\_range=cols, y\_range=cols)
> 
> > > source = ColumnDataSource(dict(x=x, y=y, color=c))  
> > > p.rect( ‘x’, ‘y’, source=source, fill\_color={‘field’:  
> > > ‘color’, ‘transform’: mapper}, line\_color=‘black’,  
> > > width=1, height=1)
> 
> > > ```
> > > p = figure( toolbar_location=None, tools='',
> > > 
> > > ```
> > > 
> > > x\_range=cols, y\_range=cols)
> 
> > > p.add\_layout( color\_bar, ‘right’)
> > > 
> > > ```
> > > #output_file(r'/tmp/test.html')
> > > output_notebook()
> > > 
> > > show(p)
> > > 
> > > ```
> 
> > > ```auto
> > > 
> > > Have a nice day,
> > > 
> > > Raphael
> > > 
> > > ```
> 
> > > ```
> > > On
> > > 
> > > ```
> > > 
> > > 2017-05-04 09:43, alEx S wrote:
> 
> > > > Hi,
> 
> > > > ```
> > > > I tried to make a basic correlation matrix with a
> > > > 
> > > > ```
> > > > 
> > > > linear color bar on the side, and got a Value Error  
> > > > with the dictionnary mapping values to  
> > > > LinearColorMaper. I don’t understand why. Any body  
> > > > could light me up ?
> 
> > > >
> 
> > > > ```
> > > > from bokeh.plotting import figure, show,
> > > > 
> > > > ```
> > > > 
> > > > output\_notebook
> 
> > > > from bokeh.palettes import Viridis256
> 
> > > > ```
> > > > from bokeh.models import ColumnDataSource,
> > > > 
> > > > ```
> > > > 
> > > > ColorBar, LinearColorMapper
> 
> > > > from bokeh.sampledata.autompg import autompg
> 
> > > >
> 
> > > > corr = autompg.corr()
> 
> > > > cols = list( corr.columns)
> 
> > > >
> 
> > > > x = y = c =
> 
> > > >
> 
> > > > for i in cols:
> 
> > > > for j in cols:
> 
> > > > x.append( j)
> 
> > > > y.append( i)
> 
> > > > c.append( abs( corr[i][j]))
> 
> > > >
> 
> > > > ```
> > > > mapper = LinearColorMapper( palette=Viridis256,
> > > > 
> > > > ```
> > > > 
> > > > low=0, high=1)
> 
> > > > colors= { ‘field’: c, ‘transform’: mapper}
> 
> > > > ```
> > > > color_bar = ColorBar( color_mapper=mapper,
> > > > 
> > > > ```
> > > > 
> > > > location=( 0, 0))
> 
> > > >
> 
> > > > ```
> > > > p = figure( toolbar_location=None, tools='',
> > > > 
> > > > ```
> > > > 
> > > > x\_range=cols, y\_range=cols)
> 
> > > > ```
> > > > p.rect( x, y, fill_color=colors,
> > > > 
> > > > ```
> > > > 
> > > > line\_color=‘black’, width=1, height=1)
> 
> > > > p.add\_layout( color\_bar, ‘right’)
> 
> > > >
> 
> > > > output\_notebook()
> 
> > > > show(p)
> 
> > > >
> 
> > > > # =\> ValueError: expected an element of either … # on p.rect( …, color=colors, …
> 
> > > >
> 
> > > > Thx
> 
> > > > alEx
> 
> > > > –
> > > > 
> > > > ```
> > > > 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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > > > 
> > > > ```
> > > > To post to this group, send email to bokeh@continuum.io.
> > > > 
> > > > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io](https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da23bc-7dba-473f-929b-7486eef2aaa0%40continuum.io?utm_medium=email&utm_source=footer).
> > > > 
> > > > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > > 
> > > ```
> > > To post to this group, send email to bokeh@continuum.io.
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/7dd2e92b-1621-8f14-95b6-4042ec5e69f7%40busino.ch](https://groups.google.com/a/continuum.io/d/msgid/bokeh/7dd2e92b-1621-8f14-95b6-4042ec5e69f7%40busino.ch?utm_medium=email&utm_source=footer).
> > > 
> > > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > 
> > ```
> > To post to this group, send email to bokeh@continuum.io.
> > 
> > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/CAJjPzf_6PiV65zhK8uFgxPq%3DnZaQVanmBaR9-torQxrbU4koHw%40mail.gmail.com](https://groups.google.com/a/continuum.io/d/msgid/bokeh/CAJjPzf_6PiV65zhK8uFgxPq%3DnZaQVanmBaR9-torQxrbU4koHw%40mail.gmail.com?utm_medium=email&utm_source=footer).
> > 
> > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout).
> > 
> > ```
> 
> [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io)  
> [bokeh@continuum.io](mailto:bokeh@continuum.io)  
> [https://groups.google.com/a/continuum.io/d/msgid/bokeh/3d1de085-c07c-7864-5bf6-400a3c62bc3f%40busino.ch](https://groups.google.com/a/continuum.io/d/msgid/bokeh/3d1de085-c07c-7864-5bf6-400a3c62bc3f%40busino.ch?utm_medium=email&utm_source=footer)  
> [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout)

---

<div class="post-metadata">

**Author:** ![Marcus\_Donnelly](https://discourse.bokeh.org/user_avatar/discourse.bokeh.org/marcus_donnelly/32/1294_2.png) [@Marcus\_Donnelly](https://discourse.bokeh.org/u/Marcus_Donnelly)\
**Post date:** [May 4, 2017, 10:20am UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/6 "2017-05-04T10:20:52Z")

</div>

Hi alEx,

Is the following the sort of thing you’re after? I’m not sure if colour mappers work with rectangle glyphs in the way you’re trying, but they do work with images which is what I use for this kind of plot…

from bokeh.plotting import figure, show

from bokeh.palettes import Viridis256  
from bokeh.models import ColorBar, LinearColorMapper  
from bokeh.sampledata.autompg import autompg

corr = autompg.corr()  
cols = list(corr.columns)

x = y =

for i in cols:  
for j in cols:  
x.append(j)  
y.append(i)

c = abs(autompg.corr().values)

mapper = LinearColorMapper(palette=Viridis256, low=0, high=1)  
color\_bar = ColorBar(color\_mapper=mapper, location=(0,0))

p = figure(toolbar\_location=None, tools=’’, x\_range=cols, y\_range=cols)  
p.image([c], x=.5, y=.5, dw=len(cols), dh=len(cols), color\_mapper=mapper)  
p.add\_layout(color\_bar, ‘right’)

show(p)

``

---

<div class="post-metadata">

**Author:** ![Alex\_Schmitt](https://discourse.bokeh.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Alex\_Schmitt](https://discourse.bokeh.org/u/Alex_Schmitt)\
**Post date:** [May 4, 2017, 2:19pm UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/7 "2017-05-04T14:19:48Z")

</div>

Thx for helping me out guys.  
At the end what was not working is that I thought that x = ;y = ; c = was equivalent to x = y = c = ☹

Now it’s working nicely ! And below is the full code with the plot and the html file for Hover interactivity at the end:

import numpy as np

import pandas as pd

from bokeh.plotting import figure, show, output\_notebook

from bokeh.palettes import Viridis256

from bokeh.models import ColumnDataSource, ColorBar, LinearColorMapper, HoverTool

from bokeh.sampledata.autompg import autompg

cols = list( autompg.corr().columns)

corr\_matrix = np.asmatrix( autompg.corr())

N = len( cols)

x = ; y = ; c = ; c\_abs =

minus = [‘-’] \* N \*\* 2

size = [‘25pt’] \* N \*\* 2

for i in range(N):

for j in range(N):

x.append( cols[j])

y.append( cols[i])

c.append( corr\_matrix[i, j])

c\_abs.append( abs( corr\_matrix[i, j]))

data = pd.DataFrame({ ‘x’: x, ‘y’: y, ‘c’:c, ‘c\_abs’: c\_abs, ‘minus’: minus, ‘size’: size})

mapper = LinearColorMapper( palette=Viridis256[:: -1], low=0, high=1)

colors= { ‘field’: ‘c\_abs’, ‘transform’: mapper}

color\_bar = ColorBar( color\_mapper=mapper, location=( 0, 0))

p = figure( toolbar\_location=‘above’, tools=‘hover’, x\_range=cols, y\_range=cols, title=“Auto MPG Correlations”)

source = ColumnDataSource( data)

p.rect( ‘x’, ‘y’, source= data, fill\_color=colors, line\_color=None, width=1, height=1)

# add a plus or a minus to indicate positive or negative correlation - colors are absolute values

p.cross( ‘x’, ‘y’, source= data.query( “c \> 0”), size=10, color=‘black’, line\_width=2)

p.text( ‘x’, ‘y’, text=‘minus’, source= data.query( “c \< 0”),

color=‘black’, text\_font\_size=‘size’, y\_offset=16, x\_offset=-5)

p.add\_layout( color\_bar, ‘right’)

p.select\_one( HoverTool).tooltips = [( 'correlation: ', ‘@c’),]

output\_notebook()

show( p)

 ![AutoMPGHeatMap.png](https://discourse-uploads.bokeh.org/original/1X/acfa394362f546a1328c8e7b0e6fcc8ceb35b7aa.png)

Have a nice day,

alEx

[AutoMPGHeatMap.html](https://discourse-uploads.bokeh.org/original/1X/cc52206953d9b56f30b10a54ef17b1635edeac6c.html) (282 KB)

> **···**
>
> On Thu, May 4, 2017 at 12:20 PM Marcus Donnelly [marcus.k.donnelly@gmail.com](mailto:marcus.k.donnelly@gmail.com) wrote:
> 
> > Hi alEx,
> > 
> > Is the following the sort of thing you’re after? I’m not sure if colour mappers work with rectangle glyphs in the way you’re trying, but they do work with images which is what I use for this kind of plot…
> > 
> > from bokeh.plotting import figure, show
> > 
> > from bokeh.palettes import Viridis256  
> > from bokeh.models import ColorBar, LinearColorMapper
> 
> > ``
> 
> > from bokeh.sampledata.autompg import autompg
> > 
> > corr = autompg.corr()  
> > cols = list(corr.columns)
> 
> > ``
> 
> > x = y =
> 
> > ``
> 
> > for i in cols:  
> > for j in cols:  
> > x.append(j)  
> > y.append(i)
> 
> > ``
> 
> > c = abs(autompg.corr().values)
> > 
> > mapper = LinearColorMapper(palette=Viridis256, low=0, high=1)  
> > color\_bar = ColorBar(color\_mapper=mapper, location=(0,0))
> > 
> > p = figure(toolbar\_location=None, tools=‘’, x\_range=cols, y\_range=cols)  
> > p.image([c], x=.5, y=.5, dw=len(cols), dh=len(cols), color\_mapper=mapper)  
> > p.add\_layout(color\_bar, ‘right’)
> > 
> > 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 [bokeh+unsubscribe@continuum.io](mailto:bokeh+unsubscribe@continuum.io).
> > 
> > To post to this group, send email to [bokeh@continuum.io](mailto:bokeh@continuum.io).
> > 
> > To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/0979b398-5274-4b71-b220-b8b35f72b94c%40continuum.io](https://groups.google.com/a/continuum.io/d/msgid/bokeh/0979b398-5274-4b71-b220-b8b35f72b94c%40continuum.io?utm_medium=email&utm_source=footer).
> > 
> > For more options, visit [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://discourse-uploads.bokeh.org/original/2X/1/14eb24f9e10e4e9b78e3f10f66c04ffa96446e5a.svg) [@system](https://discourse.bokeh.org/u/system)\
**Post date:** [July 21, 2021, 9:11pm UTC](https://discourse.bokeh.org/t/how-color-mapper-works/2171/8 "2021-07-21T21:11:54Z")

</div>


