def add_cmap( cmapdict, name ): #my_cmap = matplotlib.colors.LinearSegmentedColormap(name,cmapdict,256) #pylab.register_cmap(name=name,cmap=my_cmap) newjet = matplotlib.cm.get_cmap('jet') newjet.set_under('b') matplotlib.cm.register_cmap(name='newjet',cmap=newjet) pylab.register_cmap(name='newjet',cmap=newjet)
def add_cmap(cmapdict, name): #my_cmap = matplotlib.colors.LinearSegmentedColormap(name,cmapdict,256) #pylab.register_cmap(name=name,cmap=my_cmap) newjet = matplotlib.cm.get_cmap('jet') newjet.set_under('b') matplotlib.cm.register_cmap(name='newjet', cmap=newjet) pylab.register_cmap(name='newjet', cmap=newjet)
def register_cmaps_from_cfg(cfg, logger): """Register cmap aliases into matplotlib""" logger.debug('Registering colormaps') for name in cfg['cmaps']: cmap_name = get_cfg_cmap(cfg, name, check_aliases=False) if (name != cmap_name and cmap_name in matplotlib.cm.cmap_d.keys()): # add an alias register_cmap(name, matplotlib.cm.cmap_d[cmap_name])
def RegisterCustomColormaps(): """Adds the 'stoplight' and 'RdGn' colormaps to matplotlib's database """ import colorsys as cs # stoplight colormap Rd1 = [1.,0.,0.]; Rd2 = Rd1 Yl1 = [1.,1.,0.]; Yl2 = Yl1 Gn1 = [0.,1.,0.]; Gn2 = Gn1 val = 0.65 Rd1 = cs.rgb_to_hsv(Rd1[0],Rd1[1],Rd1[2]) Rd1 = cs.hsv_to_rgb(Rd1[0],Rd1[1],val ) Yl1 = cs.rgb_to_hsv(Yl1[0],Yl1[1],Yl1[2]) Yl1 = cs.hsv_to_rgb(Yl1[0],Yl1[1],val ) Gn1 = cs.rgb_to_hsv(Gn1[0],Gn1[1],Gn1[2]) Gn1 = cs.hsv_to_rgb(Gn1[0],Gn1[1],val ) p = 0 level1 = 0.5 level2 = 0.75 RdYlGn = {'red': ((0.0 , 0.0 ,Rd1[0]), (level1-p, Rd2[0],Rd2[0]), (level1+p, Yl1[0],Yl1[0]), (level2-p, Yl2[0],Yl2[0]), (level2+p, Gn1[0],Gn1[0]), (1.00 , Gn2[0], 0.0)), 'green': ((0.0 , 0.0 ,Rd1[1]), (level1-p, Rd2[1],Rd2[1]), (level1+p, Yl1[1],Yl1[1]), (level2-p, Yl2[1],Yl2[1]), (level2+p, Gn1[1],Gn1[1]), (1.00 , Gn2[1], 0.0)), 'blue': ((0.0 , 0.0 ,Rd1[2]), (level1-p, Rd2[2],Rd2[2]), (level1+p, Yl1[2],Yl1[2]), (level2-p, Yl2[2],Yl2[2]), (level2+p, Gn1[2],Gn1[2]), (1.00 , Gn2[2], 0.0))} plt.register_cmap(name='stoplight', data=RdYlGn) # RdGn colormap val = 0.8 Rd = cs.rgb_to_hsv(1,0,0) Rd = cs.hsv_to_rgb(Rd[0],Rd[1],val) Gn = cs.rgb_to_hsv(0,1,0) Gn = cs.hsv_to_rgb(Gn[0],Gn[1],val) RdGn = {'red': ((0.0, 0.0, Rd[0]), (0.5, 1.0 , 1.0 ), (1.0, Gn[0], 0.0 )), 'green': ((0.0, 0.0, Rd[1]), (0.5, 1.0, 1.0 ), (1.0, Gn[1], 0.0 )), 'blue': ((0.0, 0.0, Rd[2]), (0.5, 1.0, 1.0 ), (1.0, Gn[2], 0.0 ))} plt.register_cmap(name='RdGn', data=RdGn)
def set_cmap(name='viridis'): try: import matplotlib.colormaps as cmaps plt.register_cmap(name='viridis', cmap=cmaps.viridis) except ImportError: # this is the local version from . import colormaps as cmaps plt.register_cmap(name='viridis', cmap=cmaps.viridis) finally: plt.set_cmap(name) # For some reason set_cmap creates a figure, so close it. plt.close(plt.gcf())
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 input_parameters(parser): hm_param = parser.add_argument_group('Heatmap options') arg = hm_param.add_argument arg('--dpi', type=int, default=150, help="Image resolution in dpi [default 150]") arg('-l', '--log_scale', action='store_true', help="Log scale") arg('-s', '--sqrt_scale', action='store_true', help="Square root scale") arg('--no_slabels', action='store_true', help="Do not show sample labels") arg('--minv', type=float, default=None, help= "Minimum value to display in the color map [default None meaning automatic]" ) arg('--maxv', type=float, default=None, help= "Maximum value to display in the color map [default None meaning automatic]" ) arg('--no_flabels', action='store_true', help="Do not show feature labels") arg('--max_slabel_len', type=int, default=25, help="Max number of chars to report for sample labels [default 15]" ) arg('--max_flabel_len', type=int, default=25, help="Max number of chars to report for feature labels [default 15]" ) arg('--flabel_size', type=int, default=10, help="Feature label font size [default 10]") arg('--slabel_size', type=int, default=10, help="Sample label font size [default 10]") arg('--fdend_width', type=float, default=1.0, help= "Width of the feature dendrogram [default 1 meaning 100%% of default heatmap width]" ) arg('--sdend_height', type=float, default=1.0, help= "Height of the sample dendrogram [default 1 meaning 100%% of default heatmap height]" ) arg('--metadata_height', type=float, default=.05, help= "Height of the metadata panel [default 0.05 meaning 5%% of default heatmap height]" ) arg('--metadata_separation', type=float, default=.01, help= "Distance between the metadata and data panels. [default 0.001 meaning 0.1%% of default heatmap height]" ) arg('--image_size', type=float, default=8, help= "Size of the largest between width and eight size for the image in inches [default 8]" ) arg('--cell_aspect_ratio', type=float, default=1.0, help= "Aspect ratio between width and height for the cells of the heatmap [default 1.0]" ) col_maps = [ 'Accent', 'Blues', 'BrBG', 'BuGn', 'BuPu', 'Dark2', 'GnBu', 'Greens', 'Greys', 'OrRd', 'Oranges', 'PRGn', 'Paired', 'Pastel1', 'Pastel2', 'PiYG', 'PuBu', 'PuBuGn', 'PuOr', 'PuRd', 'Purples', 'RdBu', 'RdGy', 'RdPu', 'RdYlBu', 'RdYlGn', 'Reds', 'Set1', 'Set2', 'Set3', 'Spectral', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd', 'afmhot', 'autumn', 'binary', 'bone', 'brg', 'bwr', 'cool', 'copper', 'flag', 'gist_earth', 'gist_gray', 'gist_heat', 'gist_ncar', 'gist_rainbow', 'gist_stern', 'gist_yarg', 'gnuplot', 'gnuplot2', 'gray', 'hot', 'hsv', 'jet', 'ocean', 'pink', 'prism', 'rainbow', 'seismic', 'spectral', 'spring', 'summer', 'terrain', 'winter' ] + [n for n, c in Heatmap.my_colormaps] for n, c in Heatmap.my_colormaps: my_cmap = matplotlib.colors.LinearSegmentedColormap(n, c, 256) pylab.register_cmap(name=n, cmap=my_cmap) arg('-c', '--colormap', type=str, choices=col_maps, default='bbcry') arg('--bottom_c', type=str, default=None, help= "Color to use for cells below the minimum value of the scale [default None meaning bottom color of the scale]" ) arg('--top_c', type=str, default=None, help= "Color to use for cells below the maximum value of the scale [default None meaning bottom color of the scale]" ) arg('--nan_c', type=str, default=None, help="Color to use for nan cells [default None]") """
(1.0, 1.0, 0.0)), 'green': ((0.0, 0.0, 0.0), (0.25,1.0, 1.0), (0.5, 0.0, 0.0), (0.75,0.0, 0.0), (1.0, 1.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (0.25,0.6, 1.0), (0.5, 1.0, 1.0), (0.75,0.0, 0.0), (1.0, 1.0, 0.0)) } map = LinearSegmentedColormap('map', cdict) pylab.register_cmap(cmap = map) cmap = pylab.get_cmap('map') levels = numpy.linspace(0, 4, 49) g2 = [0]*numplots g2[0] = read_walls(ff, z0, 'mc') if able_to_read_file == False: matplotlib.pyplot.plot(numpy.arange(0,10,1), [0]*10, 'k') matplotlib.pyplot.suptitle('!!!!WARNING!!!!! There is data missing from this plot!', fontsize=25) savedfilename = "figs/pair-correlation-" + str(int(ff*10)) + "-" + str(int(z0*100)) + "-" + str(int(10*theta/numpy.pi)) + ".pdf" pylab.savefig(savedfilename) exit(0) rmax = len(g2[0][:,0])*dx
if os.path.exists(sockfile): os.remove(sockfile) server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) server.bind(sockfile) os.chmod(sockfile, 0775) server.listen(5) cmap = { 'red': ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)), 'green': ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)), 'blue': ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)), 'alpha': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)) } pl.register_cmap(name='custom_cmap', data=cmap) pl.figure(frameon=False, figsize=(6,3)) try: while True: conn, addr = server.accept() data = conn.recv(1024) if data.startswith('/Library/WebServer/Documents/uploads') and data.endswith('.wav'): try: input, Fs = blurt.util.readwave(data) upsample_factor = blurt.upsample_factor * Fs / float(blurt.Fs) input = blurt.audioLoopback.processInput(input, Fs, blurt.Fc, upsample_factor) Fs /= upsample_factor pl.clf() pl.axis('off') ax = pl.axes((0,0,1,1), frameon=False, axisbg=(0,0,0,0))
def input_parameters( parser ): hm_param = parser.add_argument_group('Heatmap options') arg = hm_param.add_argument arg( '--dpi', type=int, default=150, help = "Image resolution in dpi [default 150]") arg( '-l', '--log_scale', action='store_true', help = "Log scale" ) arg( '-s', '--sqrt_scale', action='store_true', help = "Square root scale" ) arg( '--no_slabels', action='store_true', help = "Do not show sample labels" ) arg( '--minv', type=float, default=None, help = "Minimum value to display in the color map [default None meaning automatic]" ) arg( '--maxv', type=float, default=None, help = "Maximum value to display in the color map [default None meaning automatic]" ) arg( '--no_flabels', action='store_true', help = "Do not show feature labels" ) arg( '--max_slabel_len', type=int, default=25, help = "Max number of chars to report for sample labels [default 15]" ) arg( '--max_flabel_len', type=int, default=25, help = "Max number of chars to report for feature labels [default 15]" ) arg( '--flabel_size', type=int, default=10, help = "Feature label font size [default 10]" ) arg( '--slabel_size', type=int, default=10, help = "Sample label font size [default 10]" ) arg( '--fdend_width', type=float, default=1.0, help = "Width of the feature dendrogram [default 1 meaning 100%% of default heatmap width]") arg( '--sdend_height', type=float, default=1.0, help = "Height of the sample dendrogram [default 1 meaning 100%% of default heatmap height]") arg( '--metadata_height', type=float, default=.05, help = "Height of the metadata panel [default 0.05 meaning 5%% of default heatmap height]") arg( '--metadata_separation', type=float, default=.01, help = "Distance between the metadata and data panels. [default 0.001 meaning 0.1%% of default heatmap height]") arg( '--image_size', type=float, default=8, help = "Size of the largest between width and eight size for the image in inches [default 8]") arg( '--cell_aspect_ratio', type=float, default=1.0, help = "Aspect ratio between width and height for the cells of the heatmap [default 1.0]") col_maps = ['Accent', 'Blues', 'BrBG', 'BuGn', 'BuPu', 'Dark2', 'GnBu', 'Greens', 'Greys', 'OrRd', 'Oranges', 'PRGn', 'Paired', 'Pastel1', 'Pastel2', 'PiYG', 'PuBu', 'PuBuGn', 'PuOr', 'PuRd', 'Purples', 'RdBu', 'RdGy', 'RdPu', 'RdYlBu', 'RdYlGn', 'Reds', 'Set1', 'Set2', 'Set3', 'Spectral', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd', 'afmhot', 'autumn', 'binary', 'bone', 'brg', 'bwr', 'cool', 'copper', 'flag', 'gist_earth', 'gist_gray', 'gist_heat', 'gist_ncar', 'gist_rainbow', 'gist_stern', 'gist_yarg', 'gnuplot', 'gnuplot2', 'gray', 'hot', 'hsv', 'jet', 'ocean', 'pink', 'prism', 'rainbow', 'seismic', 'spectral', 'spring', 'summer', 'terrain', 'winter'] + [n for n,c in Heatmap.my_colormaps] for n,c in Heatmap.my_colormaps: my_cmap = matplotlib.colors.LinearSegmentedColormap(n,c,256) pylab.register_cmap(name=n,cmap=my_cmap) arg( '-c','--colormap', type=str, choices = col_maps, default = 'bbcry' ) arg( '--bottom_c', type=str, default = None, help = "Color to use for cells below the minimum value of the scale [default None meaning bottom color of the scale]") arg( '--top_c', type=str, default = None, help = "Color to use for cells below the maximum value of the scale [default None meaning bottom color of the scale]") arg( '--nan_c', type=str, default = None, help = "Color to use for nan cells [default None]") """
[0.741388, 0.873449, 0.149561], [0.751884, 0.874951, 0.143228], [0.762373, 0.876424, 0.137064], [0.772852, 0.877868, 0.131109], [0.783315, 0.879285, 0.125405], [0.793760, 0.880678, 0.120005], [0.804182, 0.882046, 0.114965], [0.814576, 0.883393, 0.110347], [0.824940, 0.884720, 0.106217], [0.835270, 0.886029, 0.102646], [0.845561, 0.887322, 0.099702], [0.855810, 0.888601, 0.097452], [0.866013, 0.889868, 0.095953], [0.876168, 0.891125, 0.095250], [0.886271, 0.892374, 0.095374], [0.896320, 0.893616, 0.096335], [0.906311, 0.894855, 0.098125], [0.916242, 0.896091, 0.100717], [0.926106, 0.897330, 0.104071], [0.935904, 0.898570, 0.108131], [0.945636, 0.899815, 0.112838], [0.955300, 0.901065, 0.118128], [0.964894, 0.902323, 0.123941], [0.974417, 0.903590, 0.130215], [0.983868, 0.904867, 0.136897], [0.993248, 0.906157, 0.143936]] viridis = ListedColormap(_viridis_data, name='viridis') pl.register_cmap(name='viridis', cmap=viridis) #pl.set_cmap(viridis)
[0.496615, 0.826376, 0.306377], [0.506271, 0.828786, 0.300362], [0.515992, 0.831158, 0.294279], [0.525776, 0.833491, 0.288127], [0.535621, 0.835785, 0.281908], [0.545524, 0.838039, 0.275626], [0.555484, 0.840254, 0.269281], [0.565498, 0.842430, 0.262877], [0.575563, 0.844566, 0.256415], [0.585678, 0.846661, 0.249897], [0.595839, 0.848717, 0.243329], [0.606045, 0.850733, 0.236712], [0.616293, 0.852709, 0.230052], [0.626579, 0.854645, 0.223353], [0.636902, 0.856542, 0.216620], [0.647257, 0.858400, 0.209861], [0.657642, 0.860219, 0.203082], [0.668054, 0.861999, 0.196293], [0.678489, 0.863742, 0.189503], [0.688944, 0.865448, 0.182725], [0.699415, 0.867117, 0.175971], [0.709898, 0.868751, 0.169257], [0.720391, 0.870350, 0.162603], [0.730889, 0.871916, 0.156029], [0.741388, 0.873449, 0.149561], [0.751884, 0.874951, 0.143228], [0.762373, 0.876424, 0.137064], [0.772852, 0.877868, 0.131109], [0.783315, 0.879285, 0.125405], [0.793760, 0.880678, 0.120005], [0.804182, 0.882046, 0.114965], [0.814576, 0.883393, 0.110347], [0.824940, 0.884720, 0.106217], [0.835270, 0.886029, 0.102646], [0.845561, 0.887322, 0.099702], [0.855810, 0.888601, 0.097452], [0.866013, 0.889868, 0.095953], [0.876168, 0.891125, 0.095250], [0.886271, 0.892374, 0.095374], [0.896320, 0.893616, 0.096335], [0.906311, 0.894855, 0.098125], [0.916242, 0.896091, 0.100717], [0.926106, 0.897330, 0.104071], [0.935904, 0.898570, 0.108131], [0.945636, 0.899815, 0.112838], [0.955300, 0.901065, 0.118128], [0.964894, 0.902323, 0.123941], [0.974417, 0.903590, 0.130215], [0.983868, 0.904867, 0.136897], [0.993248, 0.906157, 0.143936] ] viridis = ListedColormap(_viridis_data, name='viridis') pl.register_cmap(name='viridis', cmap=viridis) #pl.set_cmap(viridis)
""" Determine the colors and mappings for the data and legend """ import pylab as p import matplotlib.colors as mpcol import numpy as np # Compatability with 1.5 matplotlib colormaps # Viridis_r is popular from . import colormaps as cmaps p.register_cmap(name='viridis_r', cmap=cmaps.viridis_r) p.register_cmap(name='magma_r', cmap=cmaps.magma_r) p.register_cmap(name='plasma_r', cmap=cmaps.plasma_r) # Custom color mappings diff_dict = { 'blue': [(0.0, 0.58203125, 0.58203125), (0.125, 0.703125, 0.703125), (0.25, 0.81640625, 0.81640625), (0.375, 0.91015625, 0.91015625), (0.4999, 0.96875, 0.96875), (0.5, 1.0, 1.0), (0.5001, 0.5625, 0.5625), (0.625, 0.37890625, 0.37890625), (0.75, 0.26171875, 0.26171875), (0.875, 0.15234375, 0.15234375), (1.0, 0.1484375, 0.1484375)], 'green': [(0.0, 0.2109375, 0.2109375), (0.125, 0.45703125, 0.45703125), (0.25, 0.67578125, 0.67578125), (0.375, 0.84765625, 0.84765625), (0.4999, 0.94921875, 0.94921875), (0.5, 1.0, 1.0), (0.5001, 0.875, 0.875), (0.625, 0.6796875, 0.6796875), (0.75, 0.42578125, 0.42578125), (0.875, 0.1875, 0.1875), (1.0, 0.0, 0.0)], 'red': [(0.0, 0.19140625, 0.19140625), (0.125, 0.26953125, 0.26953125), (0.25, 0.453125, 0.453125), (0.375, 0.66796875, 0.66796875), (0.4999, 0.875, 0.875), (0.5, 1.0, 1.0), (0.5001, 0.9921875, 0.9921875), (0.625, 0.98828125, 0.98828125), (0.75, 0.953125, 0.953125), (0.875, 0.83984375, 0.83984375), (1.0, 0.64453125, 0.64453125)]} rb_diff_cmap = mpcol.LinearSegmentedColormap('mod_diff', diff_dict) # Custom color mappings cool_dict = {'blue': [(0.0, 0.0916, 0.0916), (0.365, 1.0, 1.0), (1.0, 1.0, 1.0)], 'green': [(0.0, 0.0, 0.0), (0.365, 0.0, 0.0), (1.0, 1.0, 1.0)], 'red': [(0.0, 0.0, 0.0), (0.746, 0.0, 0.0), (1.0, 0.0, 0.0)]} cool_cmap = mpcol.LinearSegmentedColormap('mod_cool', cool_dict) class VLimit(object): """ Calculate data plot limits """
return 0 print 'Using', filename return data # COLORS cdict = { 'red': ((0.0, 0.0, 0.0), (0.25, 0.56, 0.2), (0.5, 1.0, 1.0), (0.75, 1.0, 1.0), (1.0, 1.0, 0.0)), 'green': ((0.0, 0.0, 0.0), (0.25, 1.0, 1.0), (0.5, 0.0, 0.0), (0.75, 0.0, 0.0), (1.0, 1.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (0.25, 0.6, 1.0), (0.5, 1.0, 1.0), (0.75, 0.0, 0.0), (1.0, 1.0, 0.0)) } map = LinearSegmentedColormap('map', cdict) pylab.register_cmap(cmap=map) cmap = pylab.get_cmap('map') levels = numpy.linspace(0, 4, 49) g2 = [0] * numplots g2[0] = read_walls(ff, z0, 'mc') if able_to_read_file == False: matplotlib.pyplot.plot(numpy.arange(0, 10, 1), [0] * 10, 'k') matplotlib.pyplot.suptitle( '!!!!WARNING!!!!! There is data missing from this plot!', fontsize=25) savedfilename = "figs/pair-correlation-" + str(int(ff * 10)) + "-" + str( int(z0 * 100)) + "-" + str(int(10 * theta / numpy.pi)) + ".pdf" pylab.savefig(savedfilename) exit(0)
#------------------------------------------------------------------------------- # custom cmap #------------------------------------------------------------------------------- bbcry = { 'red': ((0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.50, 0.0, 0.0), (0.75, 1.0, 1.0), (1.0, 1.0, 1.0)), 'green': ((0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.50, 1.0, 1.0), (0.75, 0.0, 0.0), (1.0, 1.0, 1.0)), 'blue': ((0.0, 0.0, 0.0), (0.25, 1.0, 1.0), (0.50, 1.0, 1.0), (0.75, 0.0, 0.0), (1.0, 0.0, 1.0)), } bbcry = mpl.colors.LinearSegmentedColormap("bbcry", bbcry, 256) pylab.register_cmap(name="bbcry", cmap=bbcry) #------------------------------------------------------------------------------- # utility functions #------------------------------------------------------------------------------- def subseq(seq, index): """numpy-style slicing and indexing for lists""" return [seq[i] for i in index] def metacolors(values, cmap): found_other = False found_none = False unique = set(values)
'blue': [(0.0, 0.0, 1.0), (1.0, 1.0, 0.0)]}], ["LightBlues",{'red':[(0.0, 1.0, 1.0), (1.0, 0.0, 1.0)], 'green': [(0.0, 1.0, 1.0), (1.0, 1.0, 1.0)], 'blue': [(0.0, 1.0, 1.0), (1.0, 1.0, 0.0)]}], ["NeonBlues",{'red':[(0.0, 1.0, 1.0), (1.0, 0.25, 1.0)], 'green': [(0.0, 1.0, 1.0), (1.0, .25, 1.0)], 'blue': [(0.0, 1.0, 1.0), (1.0, 1.0, 0.0)]}], ["NeonGreens", {'red':[(0.0, 0.0, 1.0), (1.0, .25, 1.0)], 'green': [(0.0, 0.0, 1.0),(1.0, 1.0, 1.0)], 'blue': [(0.0, 0.0, 1.0), (1.0, .25, 0.0)]}], ["Browns", {'red':[(0.0, 0.0, 1.0), (1.0, .7, 1.0)], 'green': [(0.0, 0.0, 1.0), (1.0, .45, 1.0)], 'blue': [(0.0, 0.0, 1.0), (1.0, .25, 0.0)]}]] for spec in color_map_specs: pylab.register_cmap(cmap=LinearSegmentedColormap(spec[0], spec[1])) class MyNavToolbar(NavigationToolbar): mytoolitems = (('Copy', 'Copy the figure', 'copy', 'do_copy'), ('Annotate', 'Annotate a segment', 'annotation', 'do_annotate'),) def __init__(self, canvas, win): NavigationToolbar.__init__(self, canvas, win) self.win = win path = os.path.abspath(__file__) dir_path = os.path.dirname(path) imagedir = os.path.join(dir_path,'images') for text, tooltip_text, image_file, callback in self.mytoolitems: if text is None: self.addSeparator()
def add_cmap( cmapdict, name ): my_cmap = matplotlib.colors.LinearSegmentedColormap(name,cmapdict,256) pylab.register_cmap(name=name,cmap=my_cmap)
def RegisterCustomColormaps(): """Adds the 'stoplight' and 'RdGn' colormaps to matplotlib's database """ import colorsys as cs # stoplight colormap Rd1 = [1.,0.,0.]; Rd2 = Rd1 Yl1 = [1.,1.,0.]; Yl2 = Yl1 Gn1 = [0.,1.,0.]; Gn2 = Gn1 val = 0.65 Rd1 = cs.rgb_to_hsv(Rd1[0],Rd1[1],Rd1[2]) Rd1 = cs.hsv_to_rgb(Rd1[0],Rd1[1],val ) Yl1 = cs.rgb_to_hsv(Yl1[0],Yl1[1],Yl1[2]) Yl1 = cs.hsv_to_rgb(Yl1[0],Yl1[1],val ) Gn1 = cs.rgb_to_hsv(Gn1[0],Gn1[1],Gn1[2]) Gn1 = cs.hsv_to_rgb(Gn1[0],Gn1[1],val ) p = 0 level1 = 0.5 level2 = 0.75 RdYlGn = {'red': ((0.0 , 0.0 ,Rd1[0]), (level1-p, Rd2[0],Rd2[0]), (level1+p, Yl1[0],Yl1[0]), (level2-p, Yl2[0],Yl2[0]), (level2+p, Gn1[0],Gn1[0]), (1.00 , Gn2[0], 0.0)), 'green': ((0.0 , 0.0 ,Rd1[1]), (level1-p, Rd2[1],Rd2[1]), (level1+p, Yl1[1],Yl1[1]), (level2-p, Yl2[1],Yl2[1]), (level2+p, Gn1[1],Gn1[1]), (1.00 , Gn2[1], 0.0)), 'blue': ((0.0 , 0.0 ,Rd1[2]), (level1-p, Rd2[2],Rd2[2]), (level1+p, Yl1[2],Yl1[2]), (level2-p, Yl2[2],Yl2[2]), (level2+p, Gn1[2],Gn1[2]), (1.00 , Gn2[2], 0.0))} plt.register_cmap(name='stoplight', data=RdYlGn) # RdGn colormap val = 0.8 Rd = cs.rgb_to_hsv(1,0,0) Rd = cs.hsv_to_rgb(Rd[0],Rd[1],val) Gn = cs.rgb_to_hsv(0,1,0) Gn = cs.hsv_to_rgb(Gn[0],Gn[1],val) RdGn = {'red': ((0.0, 0.0, Rd[0]), (0.5, 1.0 , 1.0 ), (1.0, Gn[0], 0.0 )), 'green': ((0.0, 0.0, Rd[1]), (0.5, 1.0, 1.0 ), (1.0, Gn[1], 0.0 )), 'blue': ((0.0, 0.0, Rd[2]), (0.5, 1.0, 1.0 ), (1.0, Gn[2], 0.0 ))} plt.register_cmap(name='RdGn', data=RdGn) # bias colormap val = 0.8 per = 0.2 /2 Rd = cs.rgb_to_hsv(1,0,0) Rd = cs.hsv_to_rgb(Rd[0],Rd[1],val) Bl = cs.rgb_to_hsv(0,0,1) Bl = cs.hsv_to_rgb(Bl[0],Bl[1],val) RdBl = {'red': ((0.0 , 0.0, Bl[0]), (0.5-per, 1.0 , 1.0 ), (0.5+per, 1.0 , 1.0 ), (1.0 , Rd[0], 0.0 )), 'green': ((0.0 , 0.0, Bl[1]), (0.5-per, 1.0 , 1.0 ), (0.5+per, 1.0 , 1.0 ), (1.0 , Rd[1], 0.0 )), 'blue': ((0.0 , 0.0, Bl[2]), (0.5-per, 1.0 , 1.0 ), (0.5+per, 1.0 , 1.0 ), (1.0 , Rd[2], 0.0 ))} plt.register_cmap(name='bias', data=RdBl) cm = LinearSegmentedColormap.from_list("wetdry",[[0.545882,0.400392,0.176078], [0.586667,0.440392,0.198824], [0.627451,0.480392,0.221569], [0.668235,0.520392,0.244314], [0.709020,0.560392,0.267059], [0.749804,0.600392,0.289804], [0.790588,0.640392,0.312549], [0.831373,0.680392,0.335294], [0.872157,0.720392,0.358039], [0.912941,0.760392,0.380784], [0.921961,0.788039,0.399020], [0.899216,0.803333,0.412745], [0.876471,0.818627,0.426471], [0.853725,0.833922,0.440196], [0.830980,0.849216,0.453922], [0.808235,0.864510,0.467647], [0.785490,0.879804,0.481373], [0.762745,0.895098,0.495098], [0.740000,0.910392,0.508824], [0.717255,0.925686,0.522549], [0.680392,0.933333,0.549020], [0.629412,0.933333,0.588235], [0.578431,0.933333,0.627451], [0.527451,0.933333,0.666667], [0.476471,0.933333,0.705882], [0.425490,0.933333,0.745098], [0.374510,0.933333,0.784314], [0.323529,0.933333,0.823529], [0.272549,0.933333,0.862745], [0.221569,0.933333,0.901961], [0.188627,0.910196,0.922157], [0.173725,0.863922,0.923333], [0.158824,0.817647,0.924510], [0.143922,0.771373,0.925686], [0.129020,0.725098,0.926863], [0.114118,0.678824,0.928039], [0.099216,0.632549,0.929216], [0.084314,0.586275,0.930392], [0.069412,0.540000,0.931569], [0.054510,0.493725,0.932745], [0.052157,0.447255,0.922549], [0.062353,0.400588,0.900980], [0.072549,0.353922,0.879412], [0.082745,0.307255,0.857843], [0.092941,0.260588,0.836275], [0.103137,0.213922,0.814706], [0.113333,0.167255,0.793137], [0.123529,0.120588,0.771569], [0.133725,0.073922,0.750000], [0.143922,0.027255,0.728431], [0.143137,0.013725,0.703922], [0.131373,0.033333,0.676471], [0.119608,0.052941,0.649020], [0.107843,0.072549,0.621569], [0.096078,0.092157,0.594118], [0.084314,0.111765,0.566667], [0.072549,0.131373,0.539216], [0.060784,0.150980,0.511765], [0.049020,0.170588,0.484314], [0.037255,0.190196,0.456863]]) plt.register_cmap("wetdry",cm)
def plot_global_figure(control_dataset, data): surf_temp_diff = data['data']['1pct']['surf_temp'] - data['data']['ctrl']['surf_temp'] precip_diff = data['data']['1pct']['precip'] - data['data']['ctrl']['precip'] f = plt.figure('global_temp_precip') f.subplots_adjust(hspace=0.35, wspace=0.28) vmin, vmax = -4., 20. zero_point = 1 - (vmax / (vmax - vmin)) p2 = 0.3 cdict = {'red': ((0.0, 0.3, 0.3), (zero_point, 0.9, 0.9), (p2, 0.8, 0.8), (1.0, 0.7, 0.7)), 'green': ((0.0, 0.5, 0.5), (zero_point, 0.9, 0.9), (p2, 0.8, 0.8), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.8, 0.8), (zero_point, 0.9, 0.9), (p2, 0.2, 0.2), (1.0, 0.0, 0.0))} cust_st_cmap = LinearSegmentedColormap('cust_st', cdict) plt.register_cmap(cmap=cust_st_cmap) plt.set_cmap('cust_st') ax1 = plt.subplot(5, 2, 1) plt.title('$\Delta$ Surface Temp.(K)\nAnnual') general_plot(control_dataset, surf_temp_diff.mean(axis=0), vmin, vmax, cbar_ticks=np.arange(-4, 21, 4)) ax2 = plt.subplot(5, 2, 3) plt.title('DJF') general_plot(control_dataset, np.roll(surf_temp_diff, 1, axis=0)[:3, :, :].mean(axis=0), vmin, vmax, cbar_ticks=np.arange(-4, 21, 4)) ax3 = plt.subplot(5, 2, 5) plt.title('JJA') general_plot(control_dataset, np.roll(surf_temp_diff, 7, axis=0)[:3, :, :].mean(axis=0), vmin, vmax, cbar_ticks=np.arange(-4, 21, 4)) ax4 = plt.subplot(5, 2, 2) vmin, vmax = -4., 4. zero_point = 1 - (vmax / (vmax - vmin)) cdict = {'blue': ((0.0, 0.1, 0.1), (zero_point, 0.9, 0.9), (1.0, 0.8, 0.8)), 'green': ((0.0, 0.4, 0.4), (zero_point, 0.9, 0.9), (1.0, 0.0, 0.0)), 'red': ((0.0, 0.8, 0.8), (zero_point, 0.9, 0.9), (1.0, 0.0, 0.0))} cust_pr_cmap = LinearSegmentedColormap('cust_pr', cdict) plt.register_cmap(cmap=cust_pr_cmap) plt.title('$\Delta$ Precipitation (mm/day)\nAnnual') general_plot(control_dataset, precip_diff.mean(axis=0), vmin, vmax, cbar_loc='right', cbar_ticks=np.arange(-4, 4.1, 2)) plt.set_cmap('cust_pr') ax5 = plt.subplot(5, 2, 4) plt.title('DJF') general_plot(control_dataset, np.roll(precip_diff, 1, axis=0)[:3, :, :].mean(axis=0), vmin, vmax, cbar_loc='right', cbar_ticks=np.arange(-4, 4.1, 2)) ax6 = plt.subplot(5, 2, 6) plt.title('JJA') general_plot(control_dataset, np.roll(precip_diff, 7, axis=0)[:3, :, :].mean(axis=0), vmin, vmax, cbar_loc='right', cbar_ticks=np.arange(-4, 4.1, 2)) ax7 = plt.subplot(5, 2, 7) plt.title('Seasonal mean temperature') plt.plot(np.arange(0.5, 11.6, 1), surf_temp_diff.mean(axis=(1, 2)), 'r-') months = [calendar.month_name[i + 1][:1] for i in range(12)] ax7.set_xticks(np.arange(12) + 1./2) ax7.set_xticklabels(months) plt.ylim((1, 4)) ax7.set_yticks(np.arange(1, 4.1, 1)) ax7.set_ylabel('$\Delta$ Temp. (K)') ax8 = plt.subplot(5, 2, 9) plt.title('Zonal mean temperature') plt.plot(np.arange(-90, 90, 180./73.), surf_temp_diff.mean(axis=(0, 2)), 'r-') ax8.set_xticks(np.arange(-90, 91, 45)) plt.xlim((-90, 90)) plt.ylim((0, 8)) ax8.set_yticks(np.arange(0, 8.1, 2)) ax8.set_ylabel('$\Delta$ Temp. (K)') def north_south_fmt(x, pos): if x > 0: return u'%1.0f°S'%abs(x) elif x == 0: return u'0°' else: return u'%1.0f°N'%abs(x) #return '%1.0fN'%abs(x) formatter = FuncFormatter(north_south_fmt) ax8.xaxis.set_major_formatter(formatter) ax9 = plt.subplot(5, 2, 8) plt.title('Seasonal mean precip.') plt.plot(np.arange(0.5, 11.6, 1), precip_diff.mean(axis=(1, 2))) months = [calendar.month_name[i + 1][:1] for i in range(12)] ax9.set_xticks(np.arange(12) + 1./2) ax9.set_xticklabels(months) plt.ylim((0.05, 0.15)) ax9.set_yticks(np.arange(0.05, 0.16, 0.05)) ax9.set_ylabel('$\Delta$ Precip. (mm/day)') ax10 = plt.subplot(5, 2, 10) plt.title('Zonal mean precip.') plt.plot(np.arange(-90, 90, 180./73.), precip_diff.mean(axis=(0, 2))) ax10.set_xticks(np.arange(-90, 91, 45)) plt.xlim((-90, 90)) plt.ylim((-1, 1)) ax10.set_yticks(np.arange(-1, 1.1, 1)) ax10.set_ylabel('$\Delta$ Precip. (mm/day)') ax10.xaxis.set_major_formatter(formatter)
""" This module provides a set of throwback colormaps. """ __author__ = "Alex Drlica-Wagner" import pylab as plt from root import root_cmap from ds9 import ds9_b # Register with matplotlib plt.register_cmap(name='root', cmap=root_cmap) plt.cm.root = plt.cm.get_cmap('root') plt.register_cmap(name='ds9_b', data=ds9_b) plt.cm.ds9_b = plt.cm.get_cmap('ds9_b') # Register here cmap_d = dict() cmap_d['root'] = plt.cm.root cmap_d['ds9_b'] = plt.cm.ds9_b locals().update(cmap_d)
def simulate_splitter(layout, wavelength=1.5, num_mod=10, discretization_res=30, north=2.0, plot=True, aspect='equal'): """ Simulates a symmetric splitter and returns the transmission and reflection Parameters ---------- layout : LayoutView layoutview of the splitter wavelength: float Wavelength of simulation in um num_mod: int number_of_modes discretization_res : int discratization resolution of the structure. north : northbound limit of the simulation plot : bool plot the fiels if True Retunrs -------- transmission,reflection absolute value of transmission and reflection """ # simulate again for the optimum wavelength camfr.set_lambda(wavelength) camfr.set_N(num_mod) # we can see convergence when increasing the number of modes to 20, 30, ... : the result doesn't change camfr.set_polarisation(camfr.TM) # TE in 3D is equivalent to TM in 2D camfr.set_upper_PML(-0.05) camfr.set_lower_wall(camfr.slab_E_wall) camfr.set_backward_modes(True) if len(layout.ports): sim_window = i3.SizeInfo(west=layout.ports["W0"].position.x, east=layout.ports["E0"].position.x, south=-0.0, north=north) else: si_layout = layout.size_info() sim_window = i3.SizeInfo(west=si_layout.west, east=si_layout.east, south=-0.0, north=north) stack_expr = camfr_stack_expr_for_structure(structure=layout, discretisation_resolution=discretization_res, window_size_info=sim_window, process_flow=i3.TECH.VFABRICATION.PROCESS_FLOW) camfr_stack = camfr.Stack(stack_expr) # run the simulation # set incident field inc = np.zeros(camfr.N()) inc[0] = 1 camfr_stack.set_inc_field(inc) camfr.set_lower_wall(camfr.slab_E_wall) camfr_stack.calc() transmission = abs(camfr_stack.T12(0, 0))**2 reflection = abs(camfr_stack.R12(0, 0))**2 if plot: # Create a coordinate mesh grid_step = 0.05 x_coords = np.arange(0.0, sim_window.width, grid_step) y_coords = np.arange(0.0, sim_window.height, grid_step) x_mesh, y_mesh = np.meshgrid(x_coords, y_coords) n = np.ndarray(shape=np.shape(x_mesh), dtype=np.complex) ################# # Extracting and plotting field profiles ################# fields = np.ndarray(shape=np.shape(x_mesh), dtype=np.complex) for ix, x in enumerate(x_coords): for iy, y in enumerate(y_coords): coord = camfr.Coord(y, 0.0, x) # different coordinate system fields[iy, ix] = camfr_stack.field(coord).H2() n[iy, ix] = camfr_stack.n(coord) # Make a red-blue colormap rb_map = {'red': ((0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 1.0, 1.0)), 'green': ((0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 1.0, 1.0), (0.5, 1.0, 1.0), (1.0, 0.0, 0.0)), } pylab.register_cmap(name='RedBlue', data=rb_map) # Plot real part of fields with red-blue colormap #################### # Overlay refractive index on field plot #################### real_field = np.real(fields) limits = [-np.max(np.abs(real_field)), np.max(np.abs(real_field))] pylab.figure() pylab.contourf(x_mesh, y_mesh, real_field, 100, cmap='RedBlue', clim=limits) pylab.contourf(x_mesh, y_mesh, np.real(n), cmap='Greys', alpha=0.1) pylab.axis('image') ax = pylab.gca() ax.set_aspect(aspect) pylab.show() return (transmission, reflection)
def load_ds9_cmap(): plt.register_cmap(name='ds9_b', data=ds9_b) plt.cm.ds9_b = plt.cm.get_cmap('ds9_b') return plt.cm.ds9_b
idx = [ 0 ] # for path in paths: try: master = sampleset.generate(os.path.join(config.master_dir, path)) except IOError: # not an image? print >> sys.stderr, "Cannot load '%s' - skipping" % path continue master = reduce_image(master, RESIZE_FACT) if not os.path.exists(master.path): master.save() master = numpy.array(master.image) masters.append(master) palette = numpy.random.rand(4000, 3) discrete_cmap = matplotlib.colors.ListedColormap(palette, name = 'discrete') pylab.register_cmap(name = 'discrete', cmap = discrete_cmap) if require('CALC_FEATURES'): with Timer("Compute the features ..."): features = [ ] for master in masters: feat = local_binary_pattern(master, 8, 1) feat = feat.astype(int) features.append(feat) if require('FEATURE_HIST'): with Timer("Compute the local feature histograms ..."): cdfs = [ ] for (master, feat) in zip(masters, features): cdf = cum_counts(feat) cdfs.append(cdf)
def RegisterCustomColormaps(): """Adds some new colormaps to matplotlib's database. """ import colorsys as cs # score colormap cm = LinearSegmentedColormap.from_list( "score", [[0.84765625, 0.37109375, 0.0078125], [0.45703125, 0.4375, 0.69921875], [0.10546875, 0.6171875, 0.46484375]]) plt.register_cmap("score", cm) # bias colormap val = 0.8 per = 0.2 / 2 Rd = cs.rgb_to_hsv(1, 0, 0) Rd = cs.hsv_to_rgb(Rd[0], Rd[1], val) Bl = cs.rgb_to_hsv(0, 0, 1) Bl = cs.hsv_to_rgb(Bl[0], Bl[1], val) RdBl = { 'red': ((0.0, 0.0, Bl[0]), (0.5 - per, 1.0, 1.0), (0.5 + per, 1.0, 1.0), (1.0, Rd[0], 0.0)), 'green': ((0.0, 0.0, Bl[1]), (0.5 - per, 1.0, 1.0), (0.5 + per, 1.0, 1.0), (1.0, Rd[1], 0.0)), 'blue': ((0.0, 0.0, Bl[2]), (0.5 - per, 1.0, 1.0), (0.5 + per, 1.0, 1.0), (1.0, Rd[2], 0.0)) } plt.register_cmap(cmap=LinearSegmentedColormap('bias', RdBl)) cm = LinearSegmentedColormap.from_list( "wetdry", [[0.545882, 0.400392, 0.176078], [0.586667, 0.440392, 0.198824], [0.627451, 0.480392, 0.221569], [0.668235, 0.520392, 0.244314], [0.709020, 0.560392, 0.267059], [0.749804, 0.600392, 0.289804], [0.790588, 0.640392, 0.312549], [0.831373, 0.680392, 0.335294], [0.872157, 0.720392, 0.358039], [0.912941, 0.760392, 0.380784], [0.921961, 0.788039, 0.399020], [0.899216, 0.803333, 0.412745], [0.876471, 0.818627, 0.426471], [0.853725, 0.833922, 0.440196], [0.830980, 0.849216, 0.453922], [0.808235, 0.864510, 0.467647], [0.785490, 0.879804, 0.481373], [0.762745, 0.895098, 0.495098], [0.740000, 0.910392, 0.508824], [0.717255, 0.925686, 0.522549], [0.680392, 0.933333, 0.549020], [0.629412, 0.933333, 0.588235], [0.578431, 0.933333, 0.627451], [0.527451, 0.933333, 0.666667], [0.476471, 0.933333, 0.705882], [0.425490, 0.933333, 0.745098], [0.374510, 0.933333, 0.784314], [0.323529, 0.933333, 0.823529], [0.272549, 0.933333, 0.862745], [0.221569, 0.933333, 0.901961], [0.188627, 0.910196, 0.922157], [0.173725, 0.863922, 0.923333], [0.158824, 0.817647, 0.924510], [0.143922, 0.771373, 0.925686], [0.129020, 0.725098, 0.926863], [0.114118, 0.678824, 0.928039], [0.099216, 0.632549, 0.929216], [0.084314, 0.586275, 0.930392], [0.069412, 0.540000, 0.931569], [0.054510, 0.493725, 0.932745], [0.052157, 0.447255, 0.922549], [0.062353, 0.400588, 0.900980], [0.072549, 0.353922, 0.879412], [0.082745, 0.307255, 0.857843], [0.092941, 0.260588, 0.836275], [0.103137, 0.213922, 0.814706], [0.113333, 0.167255, 0.793137], [0.123529, 0.120588, 0.771569], [0.133725, 0.073922, 0.750000], [0.143922, 0.027255, 0.728431], [0.143137, 0.013725, 0.703922], [0.131373, 0.033333, 0.676471], [0.119608, 0.052941, 0.649020], [0.107843, 0.072549, 0.621569], [0.096078, 0.092157, 0.594118], [0.084314, 0.111765, 0.566667], [0.072549, 0.131373, 0.539216], [0.060784, 0.150980, 0.511765], [0.049020, 0.170588, 0.484314], [0.037255, 0.190196, 0.456863]]) plt.register_cmap("wetdry", cm)