Colorbar limits that are different from the normalization

Viewed 153

I'm trying to make a scatter plot with colorbar where "0" is transparent. It's important that data that is zero has a transparent scatter point, but I don't want this transparent color bin to show up in my colorbar. I would like to limit the range of my colorbar legend to (1,14). clim(0, 14) doesn't do the job for me.

plt.figure()
cmaplist = plt.get_cmap('jet')(range(256))
cmaplist[:,-1] = [0] + list(np.ones(len(cmaplist[:-1])))
map_object = mpl.colors.LinearSegmentedColormap.from_list(name='agn_map', colors=cmaplist)
plt.register_cmap(cmap=map_object)
bounds = np.linspace(0, 14, 15)
norm = mpl.colors.BoundaryNorm(bounds, 256)

x, y, z = np.random.uniform(0, 10, 50), np.random.uniform(0, 10, 50), np.random.randint(0, 14, 50)
plt.scatter(x, y, c=z, cmap='agn_map', norm=norm, s=25, marker="*")
plt.xlabel('V-J restframe mag', fontsize=12)
plt.ylabel('U-V restframe mag', fontsize=12)
plt.colorbar()
plt.show()

I'd like to cut off the lowest color bin from the bar:

out

2 Answers

To answer my own question: this code gives me the result I wanted.

from matplotlib.colors import Normalize 
import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np

plt.figure()
cmaplist = plt.get_cmap('jet')(range(256))
cmaplist[:,-1] = [0] + list(np.ones(len(cmaplist[:-1])))
map_object = mpl.colors.LinearSegmentedColormap.from_list(name='agn_map', colors=cmaplist)
plt.register_cmap(cmap=map_object)

x, y, z = np.random.uniform(0, 10, 50), np.random.uniform(0, 10, 50), np.random.randint(0, 14, 50)
plt.scatter(x, y, c=z, cmap='agn_map', norm=Normalize(vmin=1,vmax=14), s=25, marker="*") #update
bounds = np.linspace(1, 14, 14)
plt.colorbar(ticks=np.arange(1,15), drawedges=True, boundaries=bounds) # update

plt.xlabel('V-J restframe mag', fontsize=12)
plt.ylabel('U-V restframe mag', fontsize=12)
plt.show()

enter image description here

How does the introduction of a library of normalization and minimum and maximum values solve your challenge? The following code raises only the rows to be added and the rows to be updated.

from matplotlib.colors import Normalize #add
plt.scatter(x, y, c=z, cmap='agn_map', norm=Normalize(vmin=1,vmax=14), s=25, marker="*") #update
plt.colorbar(ticks=np.arange(1,15)) # update

enter image description here

Related