我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用matplotlib.pyplot.register_cmap()。
def cmaps(): plot.register_cmap(name='viridis', cmap=colormaps.viridis) plot.register_cmap(name='magma', cmap=colormaps.magma) plot.register_cmap(name='inferno', cmap=colormaps.inferno) plot.register_cmap(name='plasma', cmap=colormaps.plasma)
def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'): ''' Function to offset the "center" of a colormap. Useful for data with a negative min and positive max and you want the middle of the colormap's dynamic range to be at zero Input ----- cmap : The matplotlib colormap to be altered start : Offset from lowest point in the colormap's range. Defaults to 0.0 (no lower ofset). Should be between 0.0 and `midpoint`. midpoint : The new center of the colormap. Defaults to 0.5 (no shift). Should be between 0.0 and 1.0. In general, this should be 1 - vmax/(vmax + abs(vmin)) For example if your data range from -15.0 to +5.0 and you want the center of the colormap at 0.0, `midpoint` should be set to 1 - 5/(5 + 15)) or 0.75 stop : Offset from highets point in the colormap's range. Defaults to 1.0 (no upper ofset). Should be between `midpoint` and 1.0. ''' cdict = { 'red': [], 'green': [], 'blue': [], 'alpha': [] } # regular index to compute the colors reg_index = np.linspace(start, stop, 257) # shifted index to match the data shift_index = np.hstack([ np.linspace(0.0, midpoint, 128, endpoint=False), np.linspace(midpoint, 1.0, 129, endpoint=True) ]) for ri, si in zip(reg_index, shift_index): r, g, b, a = cmap(ri) cdict['red'].append((si, r, r)) cdict['green'].append((si, g, g)) cdict['blue'].append((si, b, b)) cdict['alpha'].append((si, a, a)) newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict) plt.register_cmap(cmap=newcmap) return newcmap
def selectionChangeColorPalette(self,i): self.changeColor = True self.cmap2 = self.appobj.colorlist[i] #Create cmap for Satellite Water Vapor if (self.cmap2 == 'sat_WV'): self.max_val = 273. self.min_val = 163. position = [0,(195.-self.min_val)/(self.max_val-self.min_val), (223.-self.min_val)/(self.max_val-self.min_val), (243.-self.min_val)/(self.max_val-self.min_val), (261.-self.min_val)/(self.max_val-self.min_val), (263.-self.min_val)/(self.max_val-self.min_val),1] ctable_path = os.path.join(self.main_path,'utils','colortables',self.cmap2) new_cmap = create_colormap.make_cmap(ctable_path,position=position,bit=False) plt.register_cmap(cmap=new_cmap) #Create cmap for Satellite IR elif (self.cmap2 == 'sat_IR'): self.max_val = 300. self.min_val = 170. position = [0,(200.-self.min_val)/(self.max_val-self.min_val), (208.-self.min_val)/(self.max_val-self.min_val), (218.-self.min_val)/(self.max_val-self.min_val), (228.-self.min_val)/(self.max_val-self.min_val), (245.-self.min_val)/(self.max_val-self.min_val), (253.-self.min_val)/(self.max_val-self.min_val), (258.-self.min_val)/(self.max_val-self.min_val),1] ctable_path = os.path.join(self.main_path,'utils','colortables',self.cmap2) new_cmap = create_colormap.make_cmap(ctable_path,position=position,bit=False) plt.register_cmap(cmap=new_cmap) #Create cmap for Cloud Albedo elif (self.cmap2 == 'cloud_albedo'): self.max_val = 1.0 self.min_val = 0.0 position = [0,(0.2-self.min_val)/(self.max_val-self.min_val), (0.4-self.min_val)/(self.max_val-self.min_val), (0.6-self.min_val)/(self.max_val-self.min_val), (0.8-self.min_val)/(self.max_val-self.min_val), 1] ctable_path = os.path.join(self.main_path,'utils','colortables',self.cmap2) new_cmap = create_colormap.make_cmap(ctable_path,position=position,bit=False) plt.register_cmap(cmap=new_cmap) else: self.max_val = None self.min_val = None self.calTimeRange = False self.pltFxn(self.pNum) #Function to allow the user to turn on and off the lat/lon grid
def shiftedColorMap(self, cmap=None, start=0.0, midpoint=0.5, stop=1.0, name='shiftedcmap'): #From Paul H on stackoverflow ''' Function to offset the "center" of a colormap. Useful for data with a negative min and positive max and you want the middle of the colormap's dynamic range to be at zero Input ----- cmap : The matplotlib colormap to be altered start : Offset from lowest point in the colormap's range. Defaults to 0.0 (no lower ofset). Should be between 0.0 and `midpoint`. midpoint : The new center of the colormap. Defaults to 0.5 (no shift). Should be between 0.0 and 1.0. In general, this should be 1 - vmax/(vmax + abs(vmin)) For example if your data range from -15.0 to +5.0 and you want the center of the colormap at 0.0, `midpoint` should be set to 1 - 5/(5 + 15)) or 0.75 stop : Offset from highets point in the colormap's range. Defaults to 1.0 (no upper ofset). Should be between `midpoint` and 1.0. ''' cdict = { 'red': [], 'green': [], 'blue': [], 'alpha': [] } # regular index to compute the colors reg_index = np.linspace(start, stop, 257) # shifted index to match the data shift_index = np.hstack([ np.linspace(0.0, midpoint, 128, endpoint=False), np.linspace(midpoint, 1.0, 129, endpoint=True) ]) for ri, si in zip(reg_index, shift_index): r, g, b, a = cmap(ri) cdict['red'].append((si, r, r)) cdict['green'].append((si, g, g)) cdict['blue'].append((si, b, b)) cdict['alpha'].append((si, a, a)) self.newmap = matplotlib.colors.LinearSegmentedColormap(name, cdict) plt.register_cmap(cmap=self.newmap) #self.newmap = cdict #return newcmap ############################################################################### #### End PlotSlab() Object #### ############################################################################### ############################################################################### #### Begin AppForm() Object #### ###############################################################################
def remappedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'): ''' Function to offset the median value of a colormap, and scale the remaining color range. Useful for data with a negative minimum and positive maximum where you want the middle of the colormap's dynamic range to be at zero. Input ----- cmap : The matplotlib colormap to be altered start : Offset from lowest point in the colormap's range. Defaults to 0.0 (no lower ofset). Should be between 0.0 and 0.5; if your dataset mean is negative you should leave this at 0.0, otherwise to (vmax-abs(vmin))/(2*vmax) midpoint : The new center of the colormap. Defaults to 0.5 (no shift). Should be between 0.0 and 1.0; usually the optimal value is abs(vmin)/(vmax+abs(vmin)) stop : Offset from highets point in the colormap's range. Defaults to 1.0 (no upper ofset). Should be between 0.5 and 1.0; if your dataset mean is positive you should leave this at 1.0, otherwise to (abs(vmin)-vmax)/(2*abs(vmin)) ''' cdict = { 'red': [], 'green': [], 'blue': [], 'alpha': [] } # regular index to compute the colors reg_index = np.hstack([ np.linspace(start, 0.5, 128, endpoint=False), np.linspace(0.5, stop, 129) ]) # shifted index to match the data shift_index = np.hstack([ np.linspace(0.0, midpoint, 128, endpoint=False), np.linspace(midpoint, 1.0, 129) ]) for ri, si in zip(reg_index, shift_index): r, g, b, a = cmap(ri) cdict['red'].append((si, r, r)) cdict['green'].append((si, g, g)) cdict['blue'].append((si, b, b)) cdict['alpha'].append((si, a, a)) newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict) plt.register_cmap(cmap=newcmap) return newcmap