Start New Discussion within our Software Development Community

No idea how to get my scatter plot to talk to my colorbar. I have taken endless approaches and consulted every forum all to no avail. I am either very close or nowhere near, surly this should only be a one or two liner?? Please help.

import matplotlib.colors as cols
import matplotlib.cm as cm
import matplotlib.pylab as plt

norm=cols.Normalize(clip=False,vmin=-0.2,vmax=0.2)
sc=plt.scatter(alph,delt,edgecolor='none',s=5,cmap=plt.cm.get_cmap('RdYBlu',norm=norm,vmin=-0.2,vmax=0.2)
m=plt.cm.ScalarMappable(cmap=sc.map,norm=sc.norm)
m.set_array(dmag)
plt.colorbar(m)

alph and delt are numpy arrays of x and y coordinates, while dmag is numpy array of floats in the interval approx. [-0.6,0.8]. The result of running this cose is a scatter plot of black points alongside a colour bar ranging from -0.6 to 0.8. I want the scattered points to be colored according to values in dmag in the range -0.2 to 0.2 and want the colorbar over the range -0.2 to 0.2. I want the range -0.2 to 0.2 to reflect the entire colormap.

Thanks jrf.

The following now works.

    cmap=plt.cm.get_cmap('RdYBlu')
    norm=matplotlib.colors.Normalize(clip=False,vmin-0.2,vmax=0.2)
    m=plt.cm.ScalarMappable(cmap=cmap,norm=norm)
    m.set_array(dmag)
    sc=plt.scatter(alph,delt,edgecolors='none',s=5,c=m.to_rgba(dmag),cmap=cmap, norm=norm)
    plt.colorbar(m)
This question has already been answered. Start a new discussion instead.