Home Matplotlib Cheat Sheet
Post
Cancel

Matplotlib Cheat Sheet

Update Date: 2023-12-03

Custom Colorbar

from_levels_and_colors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from matplotlib.colors import from_levels_and_colors

def getCmapAndNorm(cmapName, levels, extend="neither"):
    cmap = plt.get_cmap(cmapName)
    if extend == "both":
        cmap, norm = from_levels_and_colors(
                         levels=levels,
                         colors=[cmap(i/len(levels)) for i in range(len(levels)+1)],
                         extend=extend)
    elif extend == "max" or extend == "min":
        cmap, norm = from_levels_and_colors(
                         levels=levels,
                         colors=[cmap(i/len(levels)) for i in range(len(levels))],
                         extend=extend)
    elif extend == "neither":
        cmap, norm = from_levels_and_colors(
                         levels=levels,
                         colors=[cmap(i/len(levels)) for i in range(len(levels)-1)])
    return cmap, norm

Ref: https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.from_levels_and_colors.html

subplots share one colorbar

1
2
3
4
5
import matplotlib.pyplot as plt

def addSharedColorbar(fig, left, bottom, width, height):
    cbar_ax = fig.add_axes([left, bottom, width, height])
    fig.colorbar(data, cax=cbar_ax)

colorbar title

1
2
3
fig, ax = plt.subplots()
cbar = fig.colorbar(plotsVar)
cbar.ax.set_title(colorBarTitle)

Subplots

subplots ratio

1
2
3
import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 1, sharex=True, gridspec_kw={'height_ratios': [3, 1]})
This post is licensed under CC BY 4.0 by the author.