Plot colorbar with discrete colors¶
InĀ [1]:
Copied!
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal.windows import gaussian
# import plotly.express as px
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal.windows import gaussian
# import plotly.express as px
Categorial values¶
Usage: land use
Set up test data¶
InĀ [2]:
Copied!
vals = np.array([1,3,5,16,21])
iswater = 16
data = np.zeros((5,5))
for i, val in enumerate(vals):
data[i,:] = val
plt.imshow(data)
plt.colorbar()
vals = np.array([1,3,5,16,21])
iswater = 16
data = np.zeros((5,5))
for i, val in enumerate(vals):
data[i,:] = val
plt.imshow(data)
plt.colorbar()
Out[2]:
<matplotlib.colorbar.Colorbar at 0x75cf94cb8ad0>
InĀ [3]:
Copied!
print(vals)
rgbs = mpl.cm.viridis(np.linspace(0,1,len(vals)+1))
rgbs
print(vals)
rgbs = mpl.cm.viridis(np.linspace(0,1,len(vals)+1))
rgbs
[ 1 3 5 16 21]
Out[3]:
array([[0.267004, 0.004874, 0.329415, 1. ],
[0.253935, 0.265254, 0.529983, 1. ],
[0.163625, 0.471133, 0.558148, 1. ],
[0.134692, 0.658636, 0.517649, 1. ],
[0.477504, 0.821444, 0.318195, 1. ],
[0.993248, 0.906157, 0.143936, 1. ]])
InĀ [4]:
Copied!
print(mpl.colors.to_rgb('cyan'))
idx = np.where(vals==iswater)[0][0]
rgbs[idx,:] = list(mpl.colors.to_rgb('cyan')) + [1]
print(rgbs)
cmap = mpl.colors.ListedColormap(rgbs[:-1]).with_extremes(over=rgbs[-1])
cmap
print(mpl.colors.to_rgb('cyan'))
idx = np.where(vals==iswater)[0][0]
rgbs[idx,:] = list(mpl.colors.to_rgb('cyan')) + [1]
print(rgbs)
cmap = mpl.colors.ListedColormap(rgbs[:-1]).with_extremes(over=rgbs[-1])
cmap
(0.0, 1.0, 1.0) [[0.267004 0.004874 0.329415 1. ] [0.253935 0.265254 0.529983 1. ] [0.163625 0.471133 0.558148 1. ] [0. 1. 1. 1. ] [0.477504 0.821444 0.318195 1. ] [0.993248 0.906157 0.143936 1. ]]
Out[4]:
from_list
under
bad
over
InĀ [5]:
Copied!
norm = mpl.colors.BoundaryNorm(vals,cmap.N, extend='max')
plt.imshow(data, cmap=cmap, norm=norm)
plt.colorbar()
norm = mpl.colors.BoundaryNorm(vals,cmap.N, extend='max')
plt.imshow(data, cmap=cmap, norm=norm)
plt.colorbar()
Out[5]:
<matplotlib.colorbar.Colorbar at 0x75cf94b3d6d0>
Continuous field with categorical thresholds¶
Usage: QVA, precipitation
InĀ [6]:
Copied!
# Example from QVA
thresholds = np.array([0.2,2,5,10])
print(thresholds)
colors_rgb= [
[160,210,255], # blue
[255,153,0], # orange
[255,40,0], # red
[170,0,170], # purple
]
colors = [mpl.colors.to_hex(np.array(rgb)/255) for rgb in colors_rgb]
print(colors )
cmap = mpl.colors.ListedColormap(colors)
cmap
# Example from QVA
thresholds = np.array([0.2,2,5,10])
print(thresholds)
colors_rgb= [
[160,210,255], # blue
[255,153,0], # orange
[255,40,0], # red
[170,0,170], # purple
]
colors = [mpl.colors.to_hex(np.array(rgb)/255) for rgb in colors_rgb]
print(colors )
cmap = mpl.colors.ListedColormap(colors)
cmap
[ 0.2 2. 5. 10. ] ['#a0d2ff', '#ff9900', '#ff2800', '#aa00aa']
Out[6]:
from_list
under
bad
over
Set up test data¶
InĀ [7]:
Copied!
N = 7 # kernel size
k1d = gaussian(N, std=1).reshape(N, 1)
kernel = np.outer(k1d, k1d)*11.
plt.imshow(kernel)
plt.colorbar()
N = 7 # kernel size
k1d = gaussian(N, std=1).reshape(N, 1)
kernel = np.outer(k1d, k1d)*11.
plt.imshow(kernel)
plt.colorbar()
Out[7]:
<matplotlib.colorbar.Colorbar at 0x75cf94a002d0>
Mask below 0.1 with NaN
InĀ [8]:
Copied!
# kernel_m1 = np.ma.masked_where(kernel<0.1, kernel)
kernel_m1 = np.where(kernel<0.1, np.nan, kernel)
print(kernel_m1)
plt.imshow(kernel_m1)
plt.colorbar()
# kernel_m1 = np.ma.masked_where(kernel<0.1, kernel)
kernel_m1 = np.where(kernel<0.1, np.nan, kernel)
print(kernel_m1)
plt.imshow(kernel_m1)
plt.colorbar()
[[ nan nan nan 0.12219896 nan nan
nan]
[ nan 0.20147203 0.90293498 1.48868812 0.90293498 0.20147203
nan]
[ nan 0.90293498 4.04667385 6.67183726 4.04667385 0.90293498
nan]
[ 0.12219896 1.48868812 6.67183726 11. 6.67183726 1.48868812
0.12219896]
[ nan 0.90293498 4.04667385 6.67183726 4.04667385 0.90293498
nan]
[ nan 0.20147203 0.90293498 1.48868812 0.90293498 0.20147203
nan]
[ nan nan nan 0.12219896 nan nan
nan]]
Out[8]:
<matplotlib.colorbar.Colorbar at 0x75cf948bd450>
Mask below 0.2 with NaN
InĀ [9]:
Copied!
# kernel_m1 = np.ma.masked_where(kernel<0.1, kernel)
kernel_m2 = np.where(kernel<thresholds[0], np.nan, kernel)
print(kernel_m2)
plt.imshow(kernel_m2)
plt.colorbar()
# kernel_m1 = np.ma.masked_where(kernel<0.1, kernel)
kernel_m2 = np.where(kernel
[[ nan nan nan nan nan nan
nan]
[ nan 0.20147203 0.90293498 1.48868812 0.90293498 0.20147203
nan]
[ nan 0.90293498 4.04667385 6.67183726 4.04667385 0.90293498
nan]
[ nan 1.48868812 6.67183726 11. 6.67183726 1.48868812
nan]
[ nan 0.90293498 4.04667385 6.67183726 4.04667385 0.90293498
nan]
[ nan 0.20147203 0.90293498 1.48868812 0.90293498 0.20147203
nan]
[ nan nan nan nan nan nan
nan]]
Out[9]:
<matplotlib.colorbar.Colorbar at 0x75cf9477e5d0>
InĀ [10]:
Copied!
cmap = mpl.colors.ListedColormap(colors)
cmap
cmap = mpl.colors.ListedColormap(colors)
cmap
Out[10]:
from_list
under
bad
over
without the extend=max option, the colorbar will not show the color for the pre-to-last value (red)
InĀ [11]:
Copied!
norm = mpl.colors.BoundaryNorm(thresholds,cmap.N)
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
cax=ax, orientation='horizontal', label='extend neither')
norm = mpl.colors.BoundaryNorm(thresholds,cmap.N, extend='max')
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
cax=ax, orientation='horizontal', label='extend max')
norm = mpl.colors.BoundaryNorm(thresholds,cmap.N)
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
cax=ax, orientation='horizontal', label='extend neither')
norm = mpl.colors.BoundaryNorm(thresholds,cmap.N, extend='max')
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
cax=ax, orientation='horizontal', label='extend max')
Out[11]:
<matplotlib.colorbar.Colorbar at 0x75cf9467c2d0>
InĀ [12]:
Copied!
plt.imshow(kernel_m1, cmap=cmap, norm=norm)
plt.colorbar()
plt.imshow(kernel_m1, cmap=cmap, norm=norm)
plt.colorbar()
Out[12]:
<matplotlib.colorbar.Colorbar at 0x75cf946e9e50>
InĀ [13]:
Copied!
plt.imshow(kernel_m2, cmap=cmap, norm=norm)
plt.colorbar()
plt.imshow(kernel_m2, cmap=cmap, norm=norm)
plt.colorbar()
Out[13]:
<matplotlib.colorbar.Colorbar at 0x75cf945a9950>
InĀ [14]:
Copied!
kernel = np.outer(k1d, k1d)
kernel_m2 = np.where(kernel<thresholds[0], np.nan, kernel)
fig, axs = plt.subplots(2,2, figsize=(10,10))
i = 0
for x in [1,5,10,15]:
ax = axs.flatten()[i]
p1 = ax.pcolormesh(kernel_m2*x, cmap=cmap, norm=norm)
plt.colorbar(p1, ax=ax)
ax.set_title(f'x={x}')
i+=1
kernel = np.outer(k1d, k1d)
kernel_m2 = np.where(kernel
InĀ [15]:
Copied!
kernel = np.outer(k1d, k1d)
kernel_m1 = np.where(kernel<0.1, np.nan, kernel)
fig, axs = plt.subplots(2,2, figsize=(10,10))
i = 0
for x in [1,5,10,15]:
ax = axs.flatten()[i]
p1 = ax.pcolormesh(kernel_m1*x, cmap=cmap, norm=norm)
plt.colorbar(p1, ax=ax)
ax.set_title(f'x={x}')
i+=1
kernel = np.outer(k1d, k1d)
kernel_m1 = np.where(kernel<0.1, np.nan, kernel)
fig, axs = plt.subplots(2,2, figsize=(10,10))
i = 0
for x in [1,5,10,15]:
ax = axs.flatten()[i]
p1 = ax.pcolormesh(kernel_m1*x, cmap=cmap, norm=norm)
plt.colorbar(p1, ax=ax)
ax.set_title(f'x={x}')
i+=1