コード例 #1
0
ファイル: plot.py プロジェクト: mw10178/CosmicAtWeb-0
    def _map(self, i):
        import maps
        log.debug('map of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)
        o = get_args_from(kwargs, margin = 0.05, width = 10e6, height = None, boundarylat = 50, projection = 'cyl',
                          drawcoastline = 1, drawgrid = 1, drawspecgrid = 1, drawcountries = 0, bluemarble = 0, nightshade = None)

        m = maps.drawmap(y, x, **o)
        x, y = m(x, y)

        if z is None:
            l, = plt.plot(x, y, **kwargs)
        else:
            # linestyle must not be 'none' when plotting 3D
            if 'linestyle' in kwargs and kwargs['linestyle'] == 'none':
                kwargs['linestyle'] = ':'

            o = get_args_from(kwargs, markersize = 6, cbfrac = 0.04, cblabel = self.alabel('z'))
            p = set_defaults(kwargs, zorder = 100)
            l = plt.scatter(x, y, c = z, s = o.markersize ** 2, edgecolor = 'none', **p)

            m = 6.0
            dmin, dmax = np.nanmin(z), np.nanmax(z)
            cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1)
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x))
            cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter)
            cb.set_label(o.cblabel)

        self.legend.append((l, self.llabel(i)))
コード例 #2
0
ファイル: plot.py プロジェクト: mw10178/CosmicAtWeb-0
    def _profile(self, i):
        log.debug('profile of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)
        o = get_args_from(kwargs, xerr = 0, yerr = 0)

        # make x binning
        xedges, xcenters, xwidths = get_binning(self.bins(i, 'x'), x)

        # compute avg and std for each x bin
        xx = xcenters
        xerr = 0.5 * xwidths if o.xerr else None
        yy = []
        yerr = []
        for l, u in zip(xedges[:-1], xedges[1:]):
            bindata = y[(l <= x) & (x < u)]
            yy.append(np.mean(bindata))
            yerr.append(np.std(bindata))
        if not o.yerr:
            yerr = None

        pargs = set_defaults(kwargs, capsize = 3, marker = '.', linestyle = 'none')
        l, _d, _d = plt.errorbar(xx, yy, yerr, xerr, **pargs)

        self.legend.append((l, self.llabel(i)))

        self.fit(i, xx, yy, yerr)
コード例 #3
0
ファイル: plot.py プロジェクト: mw10178/CosmicAtWeb-0
    def _xy(self, i):
        log.debug('xy plot of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)

        if x is not None:
            args = (x, y)
        else:
            args = (y,)

        if z is None:
            l, = plt.plot(*args, **kwargs)
        else:
            # linestyle must not be 'none' when plotting 3D
            if 'linestyle' in kwargs and kwargs['linestyle'] == 'none':
                kwargs['linestyle'] = ':'

            o = get_args_from(kwargs, markersize = 2, cbfrac = 0.04, cblabel = self.alabel('z'))
            l = plt.scatter(x, y, c = z, s = o.markersize ** 2, edgecolor = 'none', **kwargs)

            m = 6.0
            dmin, dmax = np.nanmin(z), np.nanmax(z)
            cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1)
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x))
            cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter)
            cb.set_label(o.cblabel)

        self.legend.append((l, self.llabel(i)))

        # fit
        self.fit(i, x, y)
コード例 #4
0
ファイル: plot.py プロジェクト: mw10178/CosmicAtWeb-0
    def _hist2d(self, i):
        log.debug('2D histogram of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)
        o = get_args_from(kwargs, style = 'color', density = False, log = False, cbfrac = 0.04, cblabel = 'bincontent', levels = 10)
        filled = 'color' in o.style or ('fill' in o.style)
        o.update(get_args_from(kwargs, hidezero = o.log or filled, colorbar = filled, clabels = not filled))

        # make binnings
        bins = self.bins(i, 'x')
        if  bins == 0:
            bins = int(1 + np.log2(len(x)))
        xedges, xcenters, xwidths = get_binning(bins, x)

        bins = self.bins(i, 'y')
        if  bins == 0:
            bins = int(1 + np.log2(len(y)))
        yedges, ycenters, ywidths = get_binning(bins, y)

        bincontents, _d1, _d2 = np.histogram2d(x, y, [xedges, yedges])
        bincontents = np.transpose(bincontents)
        assert np.all(_d1 == xedges)
        assert np.all(_d2 == yedges)

        # statsbox
        self.stats_fields2d(i, bincontents, xcenters, ycenters)

        if o.density:
            bincontents = get_density2d(bincontents, xwidths, ywidths)

        if o.hidezero:
            bincontents[bincontents == 0] = np.nan

        if o.log:
            bincontents = np.log10(bincontents)
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(np.power(10, x)))
        else:
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x))

        if 'color' in o.style:
            pargs = set_defaults(kwargs, cmap = 'jet', edgecolor = 'none')
            plt.pcolor(xedges, yedges, ma.array(bincontents, mask = np.isnan(bincontents)), **kwargs)

        elif 'box' in o.style:
            pargs = set_defaults(kwargs, color = (1, 1, 1, 0), marker = 's', edgecolor = 'k')
            n = bincontents.size
            s = bincontents.reshape(n)
            s = s / np.nanmax(s) * (72. / 2. * self.w / max(len(xcenters), len(ycenters))) ** 2
            xcenters, ycenters = np.meshgrid(xcenters, ycenters)
            plt.scatter(xcenters.reshape(n), ycenters.reshape(n), s = s, **pargs)

        elif 'contour' in o.style:
            pargs = set_defaults(kwargs, cmap = 'jet')
            if not isinstance(pargs['cmap'], mpl.colors.Colormap):
                pargs['cmap'] = mpl.cm.get_cmap(pargs['cmap'])

            if filled:
                cs = plt.contourf(xcenters, ycenters, bincontents, o.levels, **pargs)
            else:
                cs = plt.contour(xcenters, ycenters, bincontents, o.levels, **pargs)
                if o.clabels:
                    plt.clabel(cs, inline = 1)

        else:
            raise ValueError('unknown style ' + o.style)

        if o.colorbar:
            m = 6.0
            dmin, dmax = np.nanmin(bincontents), np.nanmax(bincontents)
            if o.log:
                dmin, dmax = np.ceil(dmin), np.floor(dmax) + 1
                step = max(1, np.floor((dmax - dmin) / m))
                cticks = np.arange(dmin, dmax, step)
            else:
                cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1)

            cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter)
            cb.set_label(o.cblabel)
コード例 #5
0
ファイル: plot.py プロジェクト: mw10178/CosmicAtWeb-0
    def _hist1d(self, i):
        self.plotted_lines = []
        log.debug('1D histogram of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)

        o = get_args_from(kwargs, density = False, cumulative = 0)
        o.update(get_args_from(kwargs, style = 'histline' if o.density else 'hist'))
        err = 0  # o.style.startswith('s')
        o.update(get_args_from(kwargs, xerr = err, yerr = err, capsize = 3 if err else 0))

        bins = self.bins(i, 'x')
        if  bins == 0:
            bins = int(1 + np.log2(len(x)))
        binedges, bincenters, binwidths = get_binning(bins, x)

        bincontents, _d1 = np.histogram(x, binedges)
        assert np.all(binedges == _d1)
        binerrors = np.sqrt(bincontents)
        binerrors[binerrors == 0] = 1

        # statsbox
        self.stats_fields1d(i, x, bincontents, binerrors, binedges)

        if o.density:
            bincontents, binerrors = get_density(bincontents, binerrors, binwidths)

        if o.cumulative:
            bincontents, binerrors = get_cumulative(bincontents, binerrors, o.cumulative, binwidths if o.density else 1)

        if 'line' in o.style:
            x = bincenters
            y = bincontents
        else:
            x, y = get_step_points(bincontents, binedges)

        if 'fill' in o.style:
            l, = plt.fill(x, y, **kwargs)

        elif 'hist' in o.style:
            l, = plt.plot(x, y, **kwargs)

        elif 'scat' in o.style:
            pargs = set_defaults(kwargs, linestyle = '', marker = '.')
            l, = plt.plot(bincenters, bincontents, **pargs)

        else:
            raise ValueError('unknown style: ' + o.style)

        if o.xerr or o.yerr:
            pargs = set_defaults(kwargs, capsize = o.capsize, ecolor = 'k' if 'fill' in o.style else l.get_c())
            xerr = 0.5 * binwidths if o.xerr else None
            yerr = binerrors if o.yerr else None
            plt.errorbar(bincenters, bincontents, yerr, xerr, fmt = None, **pargs)


        adjust_limits('x', binedges)
        adjust_limits('y', bincontents + binerrors, marl = 0)

        self.legend.append((l, self.llabel(i)))

        self.fit(i, bincenters, bincontents, binerrors)