# How to create a scalar bar with Matplolib colormaps?

**URL:** <https://discourse.vtk.org/t/how-to-create-a-scalar-bar-with-matplolib-colormaps/5002>\
**Category:** Development\
**Created:** [January 14, 2021, 12:33pm UTC](https://discourse.vtk.org/t/how-to-create-a-scalar-bar-with-matplolib-colormaps/5002 "2021-01-14T12:33:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MM8050](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@MM8050](https://discourse.vtk.org/u/MM8050)\
**Post date:** [January 14, 2021, 12:33pm UTC](https://discourse.vtk.org/t/how-to-create-a-scalar-bar-with-matplolib-colormaps/5002/1 "2021-01-14T12:33:45Z")

</div>

Hello everyone,

I am trying to implement a scalar bar for improving data reading. The particularity is that I want to have a specific colormap for the data and the scalar bar. I am wondering whether or not it is possible to use Matplotlib colormaps (such as “viridis”, “plasma”, …) for creating a scalar bar and how to do it?

My code (in python) is as follows:

#########################  
import vtk #version 8.1.2  
from matplotlib import cm

def set\_colors(data):  
cmap = cm.get\_cmap(‘viridis’)  
colors = cmap(data)[:, :3]  
return colors

def main():  
coordinates = [[-29, 19, -19], [-34, 19, -19], [-39, 19, -19], [-44, 19, -19],[-49, 19, -19]]  
data = [0.1, 0.2, 0.3, 0.4, 0.5]

```
renderer = vtk.vtkRenderer()
renWindow = vtk.vtkRenderWindow()
renWindow.AddRenderer(renderer)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWindow)

rgb_colors = set_colors(data)

for i in range(0, len(coordinates)):
    sphere = vtk.vtkSphereSource()
    sphere.SetCenter(coordinates[i][0],coordinates[i][1],coordinates[i][2])
    sphere.SetRadius(2)
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(sphere.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(rgb_colors[i][0], rgb_colors[i][1], rgb_colors[i][2])
    renderer.AddActor(actor)

iren.Initialize()
iren.Start()

```

if **name** == ‘ **main** ’:  
main()  
#########################

Thank for your help!
