def testVCSreset1only(self): for gtype in vcs.listelements(): b0 = vcs.listelements(gtype) if gtype == 'colormap': b = vcs.createcolormap() xtra = vcs.createisofill() untouched = vcs.listelements("isofill") elif gtype == "template": b = vcs.createtemplate() elif gtype == "textcombined": b = vcs.createtextcombined() elif gtype == "textorientation": b = vcs.createtextorientation() elif gtype == "texttable": b = vcs.createtexttable() elif gtype == "fillarea": b = vcs.createfillarea() elif gtype == "projection": b = vcs.createprojection() elif gtype == "marker": b = vcs.createmarker() elif gtype == "line": b = vcs.createline() elif gtype in ["display", "font", "fontNumber", "list", "format"]: vcs.manageElements.reset() continue else: b = vcs.creategraphicsmethod(gtype) if gtype != "colormap": xtra = vcs.createcolormap() untouched = vcs.listelements("colormap") b1 = vcs.listelements(gtype) self.assertNotEqual(b0, b1) vcs.manageElements.reset(gtype) b2 = vcs.listelements(gtype) self.assertEqual(b0, b2) if gtype == "colormap": self.assertEqual(untouched, vcs.listelements("isofill")) else: self.assertEqual(untouched, vcs.listelements("colormap")) vcs.manageElements.reset() if gtype == "colormap": self.assertNotEqual(untouched, vcs.listelements("isofill")) else: self.assertNotEqual(untouched, vcs.listelements("colormap")) # case for 1d weirdness sc = vcs.createscatter() vcs.manageElements.reset()
def matplotlib2vcs(cmap, vcs_name=None): """Convert a matplotlib colormap to a vcs colormap Input can be either the actual matplotlib colormap or its name Optional second argument: vcs_name, name of the resulting vcs colormap """ import vcs import matplotlib.cm import warnings if isinstance(cmap, (str, unicode)): try: cmap = matplotlib.cm.get_cmap(cmap) except: raise RuntimeError("Could not retrieve matplotlib colormap: %s" % cmap) if vcs_name is None: vcs_name = cmap.name i = 0 vcs_name_final = vcs_name while vcs_name_final in vcs.listelements("colormap"): vcs_name_final = vcs_name + "_mpl_%.3i" % i i += 1 if vcs_name_final != vcs_name: warnings.warn( "%s colormap name was already existing, your colormap name will be: %s" % (vcs_name, vcs_name_final)) vcs_cmap = vcs.createcolormap(vcs_name_final) cmap_rgbs = cmap(range(0, cmap.N)) for i in range(0, min(cmap.N, 256)): vcs_cmap.setcolorcell(i, *([int(x * 100) for x in cmap_rgbs[i][:4]])) return vcs_cmap
def renamed(self): newname = str(self.newname.text()) cmap = vcs.createcolormap(newname, self.colors.cmap) self.colormap.addItem(newname) self.colormap.model().sort(0) self.colormap.setCurrentIndex(self.colormap.findText(newname)) self.newname.setText("")
def createcolormap(self, name, nameSource): """Creates a colormap 'name' as a copy of 'nameSource'""" name = str(name) if (nameSource is None): nameSource = 'default' else: nameSource = str(nameSource) cm = vcs.createcolormap(name, nameSource) return list(cm.getindex().values())
def createcolormap(self, name, nameSource): """Creates a colormap 'name' as a copy of 'nameSource'""" name = str(name) if (nameSource is None): nameSource = 'default' else: nameSource = str(nameSource) cm = vcs.createcolormap(name, nameSource) return cm.getindex().values()
def get_colormap(colormap, parameters): """Get the colormap (string, list for vcs, or mpl colormap obj), which can be loaded from a local file in the cwd, installed file, or a predefined mpl/vcs one.""" if not colormap.endswith('.rgb'): # predefined vcs/mpl colormap return colormap installed_colormap = os.path.join(sys.prefix, 'share', 'acme_diags', 'colormaps', colormap) if os.path.exists(colormap): # colormap is an .rgb in the current directory pass elif not os.path.exists(colormap) and os.path.exists(installed_colormap): # use the colormap from /plot/colormaps colormap = installed_colormap elif not os.path.exists(colormap) and not os.path.exists(installed_colormap): pth = os.path.join(sys.prefix, 'share', 'acme_diags', 'colormaps') msg = "File {} isn't in the current working directory or installed in {}" raise IOError(msg.format(colormap, pth)) rgb_arr = numpy.loadtxt(colormap) if parameters.backend in ['cartopy', 'mpl', 'matplotlib']: from matplotlib.colors import LinearSegmentedColormap rgb_arr = rgb_arr / 255.0 cmap = LinearSegmentedColormap.from_list(name=colormap, colors=rgb_arr) return cmap elif parameters.backend in ['vcs']: from vcs import createcolormap rgb_arr = rgb_arr / 2.55 cmap = createcolormap() cmap_range = range(len(rgb_arr)) for i in cmap_range: r, g, b = rgb_arr[i] cmap.setcolorcell(i, r, g, b, 100) return cmap, cmap_range else: raise RuntimeError('Invalid backend: {}'.format(parameters.backend))
def matplotlib2vcs(cmap, vcs_name=None): """ Convert a matplotlib colormap to a vcs colormap Input can be either the actual matplotlib colormap or its name Optional second argument: vcs_name, name of the resulting vcs colormap :param cmap: A matplotlib colormap or string name of a matplotlib colormap :type cmap: :py:class:`str` , matplotlib.cm :param vcs_name: String to set the name of the generated VCS colormap :type vcs_name: :py:class:`str` :returns: A VCS colormap object :rtype: vcs.colormap.Cp """ import vcs import matplotlib.cm import warnings if isinstance(cmap, (str, unicode)): try: cmap = matplotlib.cm.get_cmap(cmap) except: raise RuntimeError("Could not retrieve matplotlib colormap: %s" % cmap) if vcs_name is None: vcs_name = cmap.name i = 0 vcs_name_final = vcs_name while vcs_name_final in vcs.listelements("colormap"): vcs_name_final = vcs_name + "_mpl_%.3i" % i i += 1 if vcs_name_final != vcs_name: warnings.warn( "%s colormap name was already existing, your colormap name will be: %s" % (vcs_name, vcs_name_final)) vcs_cmap = vcs.createcolormap(vcs_name_final) cmap_rgbs = cmap(range(0, cmap.N)) for i in range(0, min(cmap.N, 256)): vcs_cmap.setcolorcell(i, *([int(x * 100) for x in cmap_rgbs[i][:4]])) return vcs_cmap
import vcs, cdms2 f = cdms2.open(vcs.sample_data + '/clt.nc') s = f("clt", squeeze=1, time="1979-1") zeroed = cdms2.createVariable([[0 for _ in range(s.shape[1])] for _ in range(s.shape[0])], axes=s.getAxisList(), grid=s.getGrid()) x = vcs.init() box = vcs.createboxfill() box.boxfill_type = "custom" box.levels = [1, 100] box.fillareacolors = [242] light = vcs.createline() colormap = vcs.createcolormap() colormap.index[1] = [10, 10, 10] light.colormap = colormap light.type = "dash-dot" dark = vcs.createline() dark.width = 2 justdata = vcs.createtemplate() justdata.blank() justdata.data.priority = 1 x.plot(s, continents=4, continents_line=light, bg=1) x.plot(zeroed, justdata, box, continents=1, continents_line=dark, bg=1) x.png("nice_continents")
d_jja.id = 'pr' d_jja.model = 'HadGEM2-AO' #================================================================================================ # Plot #------------------------------------------------------------------------------------------------ # Create canvas --- canvas = vcs.init() canvas.open() # Set plot type --- iso = canvas.createisofill() # Color setup --- cmap = vcs.createcolormap( "my_colormap", "rainbow" ) #you can specify which colormap you want to copy from in the second argument for i in range(255): r, g, b, _ = cmap.getcolorcell(i) cmap.setcolorcell(i, r, g, b, 50) # Transparency: 0-100 canvas.setcolormap(cmap) levels = list(range(3, 33, 3)) # [3, 6, 9, 12, 15, 18, 21, 24, 27, 30] levels.append(1.e20) iso.levels = levels iso.missing = (0, 0, 0, 0) colors = vcs.getcolors(levels, colors=range(16, 240)) iso.fillareacolors = colors # Plot --- canvas.plot(d_jja, iso)
#================================================================================================ # Plot #------------------------------------------------------------------------------------------------ # Create canvas --- canvas = vcs.init(geometry=(1200,800)) canvas.open() # Set plot type --- iso = canvas.createisofill() # Color setup --- levs = range(3,33,3) # [3, 6, 9, 12, 15, 18, 21, 24, 27, 30] iso.levels = levs iso.ext_2 ="y" cmap = vcs.createcolormap("my_colormap", "rainbow") #you can specify which colormap you want to copy from in the second argument for i in range(255): r, g, b, _ = cmap.getcolorcell(i) cmap.setcolorcell(i, r, g, b, 50) # Transparency: 0-100 canvas.setcolormap(cmap) # Plot --- canvas.plot(d_jja,iso) # Set title --- plot_title = vcs.createtext() plot_title.x = .5 plot_title.y = .98 plot_title.height = 24 plot_title.halign = "center" plot_title.valign = "top"
def cmap(self, value): if not vcs.iscolormap(value): value = vcs.getcolormap(value) self._real_map = value self._cmap = vcs.createcolormap(Cp_name_src=self._real_map.name) self.update_table()
import vcs, cdms2, os vcs.download_sample_data_files() colormap = vcs.createcolormap("transparent") for i in range(5, 240): percent = int(100 * (i - 5) / 234.) colormap.index[i][3] = percent canvas = vcs.init() canvas.setcolormap(colormap) cltfile = cdms2.open(os.path.join(vcs.sample_data, "clt.nc")) clt = cltfile("clt") canvas.plot(clt) canvas.png("rgba_colormap")