Skip to content

pratiman-91/colormaps

Repository files navigation

Colormaps

Colormaps is a library of collection of colormaps or color palettes for Python. It's written in Python with matplotlib and numpy as dependencies. You can use Colormaps to customize matplotlib plots.

Colormaps has colormaps or color palettes from:

Getting started

Dependencies

Python with:

  • matplotlib
  • numpy

Installation

pip install colormaps

or using conda

conda install colormaps -c conda-forge`

or using mamba

mamba install colormaps

or you can also use GitHub repo

git clone https://github.com/pratiman-91/colormaps.git
cd colormaps
python setup.py install

Using Colormaps

  • Importing Colormaps
import colormaps as cmaps
cmaps.drought_severity

brwnyl

cmaps.ice

ice

  • Reverse the colormap
cmaps.ice_r

ice_r

  • Getting discrete number of levels
cmaps.ice.discrete(10)

ice_discrete

  • Shifting the colormap
cmaps.ice.shift(0.5)

ice_shift

  • Shifting and then discrete levels
cmaps.ice.shift(0.5).discrete(10)

ice_shift_discrete

  • Discrete levels then cut the colormap from left side
cmaps.ice.discrete(11).cut(0.25, 'left')

ice_shift_discrete

  • Concatenate two or more colormaps
from colormaps.utils import concat
concat1 = concat(["ice", "BkBlAqGrYeOrReViWh200"])

concat_1

  • Concatenate two or more colormaps based on ratio
from colormaps.utils import concat
concat2 = concat([cmaps.ice, cmaps.BkBlAqGrYeOrReViWh200], ratios=[0.25,0.75])

concat_2

  • Concatenate two or more colormaps with granular support
from colormaps.utils import concat
concat3 = concat(
        ["ice", "thermal"],
        ratios=[0.4, 0.6],
        trim=[0.1, 0.05],
        discrete=128,
        name="my_concat"
    )

my_concat

  • Matplotlib usage example
import matplotlib.pyplot as plt
import colormaps as cmaps
import numpy as np

x = y = np.arange(-3.0, 3.01, 0.05)
X, Y = np.meshgrid(x, y)

sigmax = sigmay = 1.0
mux = muy = sigmaxy=0.0

Xmu = X-mux
Ymu = Y-muy

rho = sigmaxy/(sigmax*sigmay)
z = Xmu**2/sigmax**2 + Ymu**2/sigmay**2 - 2*rho*Xmu*Ymu/(sigmax*sigmay)
denom = 2*np.pi*sigmax*sigmay*np.sqrt(1-rho**2)
Z = np.exp(-z/(2*(1-rho**2))) / denom

plt.pcolormesh(X,Y,Z,cmap=cmaps.cubehelix3_16_r)
plt.colorbar()

matplotlib_1

  • Using concat in matplotlib
# Create sample data
X = np.linspace(-np.pi, np.pi, 100)
Y = np.linspace(-np.pi, np.pi, 100)
X, Y = np.meshgrid(X, Y)
Z = np.sin(X) * np.cos(Y)

# Plot with a colormap
fig, axes = plt.subplots(1, 2, figsize=(10, 4))

# Using ice colormap
im1 = axes[0].pcolormesh(X, Y, Z, cmap=cmaps.ice, shading='auto')
axes[0].set_title("Using cmaps.ice")
plt.colorbar(im1, ax=axes[0])

# Using a custom concatenated colormap
custom_cmap = concat(["thermal", "ice"], ratios=[0.4, 0.6])
im2 = axes[1].pcolormesh(X, Y, Z, cmap=custom_cmap, shading='auto')
axes[1].set_title("Using concat(['thermal', 'ice'])")
plt.colorbar(im2, ax=axes[1])

matplotlib_2

  • Register maps with matplotlib
_ = cmaps.ice      # registers "ice" with matplotlib
_ = cmaps.thermal  # registers "thermal" with matplotlib

X = np.linspace(-np.pi, np.pi, 100)
Y = np.linspace(-np.pi, np.pi, 100)
X, Y = np.meshgrid(X, Y)
Z = np.sin(X) * np.cos(Y)

fig, axes = plt.subplots(1, 2, figsize=(10, 4))

im1 = axes[0].pcolormesh(X, Y, Z, cmap="ice", shading='auto')
axes[0].set_title('cmap="ice" (registered by colormaps)')
plt.colorbar(im1, ax=axes[0])

im2 = axes[1].pcolormesh(X, Y, Z, cmap="thermal", shading='auto')
axes[1].set_title('cmap="thermal" (registered by colormaps)')
plt.colorbar(im2, ax=axes[1])

plt.tight_layout()

matplotlib_3

  • Register collections with matplotlib
# Register a single collection up front
cmaps.register_collection('cmocean')

# Now use by string name without prior attribute access
X = np.linspace(0, 1, 100)
Y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(X, Y)
Z = np.sin(np.pi * X) * np.cos(np.pi * Y)

fig, axes = plt.subplots(1, 2, figsize=(10, 4))
im1 = axes[0].pcolormesh(X, Y, Z, cmap="ice", shading='auto')
axes[0].set_title('cmap="ice" via register_collection("cmocean")')
plt.colorbar(im1, ax=axes[0])

cmaps.register_all()

im2 = axes[1].pcolormesh(X, Y, Z, cmap="amber", shading='auto')
axes[1].set_title('cmap="amber" via register_all()')
plt.colorbar(im2, ax=axes[1])

plt.tight_layout()

matplotlib_4

Finding Colormaps

Colormaps are pre-built and loaded at the time of importing.

  • Show different collections
from colormaps.utils import show_cmaps_collection
show_cmaps_collection(collection='cmasher')

show_cmaps_collection

  • Show all collections
from colormaps.utils import show_cmaps_all
show_cmaps_all()

This is just a sample! You will get a long list of all possible colormap collections.

show_cmaps_all

About

Colormaps is a library of collection of colormaps or color palettes for Python.

Resources

License

Stars

Watchers

Forks

Contributors