def test_edit_colormap_02_custom(self): cmap = get_cmap('Blues') dialog = pswc.ColormapDialog(cmap) self.assertEqual(dialog.table.rowCount(), 1) dialog.table.selectRow(0) self.assertIsInstance(dialog.table.chosen_colormap, mcol.Colormap) self.assertColormapEqual(dialog.table.chosen_colormap, 'Blues') dialog.close()
def assertColormapEqual(self, cmap, reference, force_string=False): """Test whether two colormaps are the same Parameters ---------- cmap: str or :class:`matplotlib.colors.Colormap` The colormap to test reference: str or :class:`matplotlib.colors.Colormap` The reference colormap""" if force_string or (isstring(cmap) and isstring(reference)): self.assertEqual(cmap, reference) if isstring(cmap): cmap = get_cmap(cmap) if isstring(reference): reference = get_cmap(reference) colors = np.linspace(0, 1, 4) self.assertAlmostArrayEqual(cmap(colors), reference(colors))
def test_choose_cmap(self): fmt_w = self.fmt_widget fmt_w.fmt_widget.choose_cmap('Blues') self.assertColormapEqual(fmt_w.get_obj(), 'Blues', True) cmap = get_cmap('Blues') fmt_w.fmt_widget.choose_cmap(cmap) chosen = fmt_w.get_obj() self.assertIsInstance(chosen, mcol.Colormap) self.assertColormapEqual(chosen, cmap)
def test_show_colormap_02_custom(self): fmt_w = self.fmt_widget cmap = get_cmap('Blues') fmt_w.fmt_widget.choose_cmap(cmap) dialog = fmt_w.fmt_widget.show_cmap() self.assertIsInstance(dialog, pswc.ColormapDialog) self.assertEqual(dialog.table.rowCount(), 1) dialog.table.selectRow(0) self.assertIsInstance(dialog.table.chosen_colormap, mcol.Colormap) self.assertColormapEqual(dialog.table.chosen_colormap, 'Blues') dialog.close()
def set_colors(self, N=None, names=None): self.names = names = names or self.names self.N = N = N or self.N colors = np.zeros((len(names), N, 4)) a = np.linspace(0, 1, N) for i, cmap in enumerate(map(lambda name: psc.get_cmap(name, N), names)): colors[i, :, :] = cmap(a) self.color_da = xr.DataArray( colors, coords={'cmap': list(map(str, names))}, dims=('cmap', 'color', 'rgba'))
def create_cmap_thumb(cmap, output=None): from matplotlib.figure import Figure from matplotlib.cm import ScalarMappable fig = Figure(figsize=(4., 0.2)) cax = fig.add_axes([0, 0, 1, 1]) _cmap = psc.get_cmap(cmap) mappable = ScalarMappable(cmap=_cmap) mappable.set_array([]) fig.colorbar(mappable, cmap=_cmap, cax=cax, orientation='horizontal') if output: fig.savefig(output, dpi=72) return fig
def __init__(self, names=[], N=10, *args, **kwargs): """ Parameters ---------- %(show_colormaps.parameters.no_show|use_qt)s Other Parameters ---------------- ``*args, **kwargs`` Anything else that is passed to the QAbstractTableModel """ super(ColormapModel, self).__init__(*args, **kwargs) names = _get_cmaps(names) self.names = names colors = np.zeros((len(names), N, 4)) a = np.linspace(0, 1, N) for i, cmap in enumerate(map(lambda name: get_cmap(name, N), names)): colors[i, :, :] = cmap(a) self.color_da = xr.DataArray( colors, coords={'cmap': list(map(str, names))}, dims=('cmap', 'color', 'rgba'))