Is there a way to print (or plot) a map key in the console (or on the map) in google earth engine ? What would be an approach?

var img= ee.Image('COPERNICUS/S2....................');
var ndvi = im.normalizedDifference(['B8', 'B4'])
var ndvi_brg = {min:-1, max: 1, palette: ['blue','#8B0000','green']};
Map.addLayer(ndvi.clip(rect), ndvi_brg, 'ndvi',true);

I'm ploting an NDVI in a specific area and would like to print the map key for the color palette 'ndvi_brg' as a visual clue (Like something in the following picture:) enter image description here

1 Answer 1


The annotation capabilties of Earth Engine are currently pretty limited, but you can fake a colorbar with something like this:

function ColorBar(palette) {
  return ui.Thumbnail({
    image: ee.Image.pixelLonLat().select(0),
    params: {
      bbox: [0, 0, 1, 0.1],
      dimensions: '200x20',
      format: 'png',
      min: 0,
      max: 1,
      palette: palette,
    style: {stretch: 'horizontal', margin: '0px 8px'},

function makeLegend(low, mid, high, palette) {
  var labelPanel = ui.Panel(
        ui.Label(low, {margin: '4px 8px'}),
        ui.Label(mid, {margin: '4px 8px', textAlign: 'center', stretch: 'horizontal'}),
        ui.Label(high, {margin: '4px 8px'})
  return ui.Panel([ColorBar(palette), labelPanel])

Map.add(makeLegend(-1, 0, 1, ["yellow", "orange", "green", "white"]))

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.