colormaps module¶
¶
Module for commonly used colormaps and palettes for visualizing Earth Engine data.
get_palette(cmap_name=None, n_class=None)
¶
Get a palette from a matplotlib colormap. See the list of colormaps at https://matplotlib.org/stable/tutorials/colors/colormaps.html.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmap_name |
str |
The name of the matplotlib colormap. Defaults to None. |
None |
n_class |
int |
The number of colors. Defaults to None. |
None |
Returns:
Type | Description |
---|---|
list |
A list of hex colors. |
Source code in geemap/colormaps.py
def get_palette(cmap_name=None, n_class=None):
"""Get a palette from a matplotlib colormap. See the list of colormaps at https://matplotlib.org/stable/tutorials/colors/colormaps.html.
Args:
cmap_name (str, optional): The name of the matplotlib colormap. Defaults to None.
n_class (int, optional): The number of colors. Defaults to None.
Returns:
list: A list of hex colors.
"""
cmap = plt.cm.get_cmap(cmap_name, n_class)
colors = [mpl.colors.rgb2hex(cmap(i))[1:] for i in range(cmap.N)]
return colors
list_colormaps()
¶
List all available colormaps. See a complete lost of colormaps at https://matplotlib.org/stable/tutorials/colors/colormaps.html.
Returns:
Type | Description |
---|---|
list |
The list of colormap names. |
Source code in geemap/colormaps.py
def list_colormaps():
"""List all available colormaps. See a complete lost of colormaps at https://matplotlib.org/stable/tutorials/colors/colormaps.html.
Returns:
list: The list of colormap names.
"""
return plt.colormaps()
plot_colormap(cmap, width=8.0, height=0.4, orientation='horizontal', axis_off=True, show_name=False, font_size=12)
¶
Plot a colormap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmap |
str |
The name of the colormap. |
required |
width |
float |
The width of the colormap. Defaults to 8.0. |
8.0 |
height |
float |
The height of the colormap. Defaults to 0.4. |
0.4 |
orientation |
str |
The orientation of the colormap. Defaults to "horizontal". |
'horizontal' |
axis_off |
bool |
Whether to turn axis off. Defaults to True. |
True |
show_name |
bool |
Whether to show the colormap name. Defaults to False. |
False |
font_size |
int |
Font size of the text. Defaults to 12. |
12 |
Source code in geemap/colormaps.py
def plot_colormap(
cmap,
width=8.0,
height=0.4,
orientation="horizontal",
axis_off=True,
show_name=False,
font_size=12,
):
"""Plot a colormap.
Args:
cmap (str): The name of the colormap.
width (float, optional): The width of the colormap. Defaults to 8.0.
height (float, optional): The height of the colormap. Defaults to 0.4.
orientation (str, optional): The orientation of the colormap. Defaults to "horizontal".
axis_off (bool, optional): Whether to turn axis off. Defaults to True.
show_name (bool, optional): Whether to show the colormap name. Defaults to False.
font_size (int, optional): Font size of the text. Defaults to 12.
"""
fig, ax = plt.subplots(figsize=(width, height))
col_map = plt.get_cmap(cmap)
mpl.colorbar.ColorbarBase(ax, cmap=col_map, orientation=orientation)
if axis_off:
ax.set_axis_off()
if show_name:
pos = list(ax.get_position().bounds)
x_text = pos[0] - 0.01
y_text = pos[1] + pos[3] / 2.0
fig.text(x_text, y_text, cmap, va="center", ha="right", fontsize=font_size)
plt.show()
plot_colormaps(width=8.0, height=0.4)
¶
Plot all availabe colormaps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
width |
float |
Width of the colormap. Defaults to 8.0. |
8.0 |
height |
float |
Height of the colormap. Defaults to 0.4. |
0.4 |
Source code in geemap/colormaps.py
def plot_colormaps(width=8.0, height=0.4):
"""Plot all availabe colormaps.
Args:
width (float, optional): Width of the colormap. Defaults to 8.0.
height (float, optional): Height of the colormap. Defaults to 0.4.
"""
cmap_list = list_colormaps()
nrows = len(cmap_list)
fig, axes = plt.subplots(nrows=nrows, figsize=(width, height * nrows))
fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
for ax, name in zip(axes, cmap_list):
ax.imshow(gradient, aspect="auto", cmap=plt.get_cmap(name))
ax.set_axis_off()
pos = list(ax.get_position().bounds)
x_text = pos[0] - 0.01
y_text = pos[1] + pos[3] / 2.0
fig.text(x_text, y_text, name, va="center", ha="right", fontsize=12)
# Turn off *all* ticks & spines, not just the ones with colormaps.
for ax in axes:
ax.set_axis_off()
plt.show()