08 legend and colorbar
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
In [2]:
Copied!
import ee
import geemap
import ee
import geemap
Show available built-in legends
In [3]:
Copied!
legends = geemap.builtin_legends
for legend in legends:
print(legend)
legends = geemap.builtin_legends
for legend in legends:
print(legend)
NLCD ESA_WorldCover ESRI_LandCover NWI MODIS/051/MCD12Q1 MODIS/006/MCD12Q1 GLOBCOVER JAXA/PALSAR Oxford AAFC/ACI COPERNICUS/CORINE/V20/100m COPERNICUS/Landcover/100m/Proba-V/Global USDA/NASS/CDL
Add a built-in legend to the map.
In [4]:
Copied!
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_basemap('HYBRID')
nlcd = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')
landcover = nlcd.select('landcover')
Map.addLayer(landcover, {}, 'NLCD Land Cover 2019')
Map.add_legend(legend_title="NLCD Land Cover Classification", builtin_legend='NLCD')
Map
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_basemap('HYBRID')
nlcd = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')
landcover = nlcd.select('landcover')
Map.addLayer(landcover, {}, 'NLCD Land Cover 2019')
Map.add_legend(legend_title="NLCD Land Cover Classification", builtin_legend='NLCD')
Map
Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Adding a custom legend by defining a legend dictionary with labels as keys and hex color codes as values.
In [5]:
Copied!
Map = geemap.Map(center=[40, -100], zoom=4)
legend_dict = {
'11 Open Water': '466b9f',
'12 Perennial Ice/Snow': 'd1def8',
'21 Developed, Open Space': 'dec5c5',
'22 Developed, Low Intensity': 'd99282',
'23 Developed, Medium Intensity': 'eb0000',
'24 Developed High Intensity': 'ab0000',
'31 Barren Land (Rock/Sand/Clay)': 'b3ac9f',
'41 Deciduous Forest': '68ab5f',
'42 Evergreen Forest': '1c5f2c',
'43 Mixed Forest': 'b5c58f',
'51 Dwarf Scrub': 'af963c',
'52 Shrub/Scrub': 'ccb879',
'71 Grassland/Herbaceous': 'dfdfc2',
'72 Sedge/Herbaceous': 'd1d182',
'73 Lichens': 'a3cc51',
'74 Moss': '82ba9e',
'81 Pasture/Hay': 'dcd939',
'82 Cultivated Crops': 'ab6c28',
'90 Woody Wetlands': 'b8d9eb',
'95 Emergent Herbaceous Wetlands': '6c9fb8',
}
nlcd = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')
landcover = nlcd.select('landcover')
Map.addLayer(landcover, {}, 'NLCD Land Cover 2019')
Map.add_legend(legend_title="NLCD Land Cover Classification", legend_dict=legend_dict)
Map
Map = geemap.Map(center=[40, -100], zoom=4)
legend_dict = {
'11 Open Water': '466b9f',
'12 Perennial Ice/Snow': 'd1def8',
'21 Developed, Open Space': 'dec5c5',
'22 Developed, Low Intensity': 'd99282',
'23 Developed, Medium Intensity': 'eb0000',
'24 Developed High Intensity': 'ab0000',
'31 Barren Land (Rock/Sand/Clay)': 'b3ac9f',
'41 Deciduous Forest': '68ab5f',
'42 Evergreen Forest': '1c5f2c',
'43 Mixed Forest': 'b5c58f',
'51 Dwarf Scrub': 'af963c',
'52 Shrub/Scrub': 'ccb879',
'71 Grassland/Herbaceous': 'dfdfc2',
'72 Sedge/Herbaceous': 'd1d182',
'73 Lichens': 'a3cc51',
'74 Moss': '82ba9e',
'81 Pasture/Hay': 'dcd939',
'82 Cultivated Crops': 'ab6c28',
'90 Woody Wetlands': 'b8d9eb',
'95 Emergent Herbaceous Wetlands': '6c9fb8',
}
nlcd = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')
landcover = nlcd.select('landcover')
Map.addLayer(landcover, {}, 'NLCD Land Cover 2019')
Map.add_legend(legend_title="NLCD Land Cover Classification", legend_dict=legend_dict)
Map
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Adding a colorbar¶
Add data to the map.
In [6]:
Copied!
Map = geemap.Map()
dem = ee.Image('USGS/SRTMGL1_003')
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
Map.addLayer(dem, vis_params, 'SRTM DEM')
Map
Map = geemap.Map()
dem = ee.Image('USGS/SRTMGL1_003')
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
Map.addLayer(dem, vis_params, 'SRTM DEM')
Map
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add a horizontal colorbar to the map.
In [7]:
Copied!
Map.add_colorbar(vis_params, label="Elevation (m)", layer_name="SRTM DEM")
Map.add_colorbar(vis_params, label="Elevation (m)", layer_name="SRTM DEM")
Add a vertical colorbar the map. Note that the colorbar is tied to the layer name. Note that the previously added colorbar with the same layer name will be replaced with the new colorbar.
In [8]:
Copied!
Map.add_colorbar(
vis_params, label="Elevation (m)", layer_name="SRTM DEM", orientation="vertical"
)
Map.add_colorbar(
vis_params, label="Elevation (m)", layer_name="SRTM DEM", orientation="vertical"
)
Make the colorbar background transparent.
In [9]:
Copied!
Map.add_colorbar(
vis_params,
label="Elevation (m)",
layer_name="SRTM DEM",
orientation="vertical",
transparent_bg=True,
)
Map.add_colorbar(
vis_params,
label="Elevation (m)",
layer_name="SRTM DEM",
orientation="vertical",
transparent_bg=True,
)
Add a discreate colorbar.
In [10]:
Copied!
Map.add_colorbar(
vis_params,
label="Elevation (m)",
layer_name="SRTM DEM",
orientation="vertical",
transparent_bg=True,
discrete=True,
)
Map
Map.add_colorbar(
vis_params,
label="Elevation (m)",
layer_name="SRTM DEM",
orientation="vertical",
transparent_bg=True,
discrete=True,
)
Map
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Last update:
2022-03-25