Example #1
0
    def _plot_2d(self, cmap='winter', vmin=None, vmax=None):
        points = [(m, l, [i,j]) 
                  for i, m in enumerate(self._mu2)
                  for j, l in enumerate(self._lambda)]
        result = self._parallel_eval(points)
        value_min = vmin if vmin is not None else min(r[2] for r in result)
        value_max = vmax if vmax is not None else max(r[2] for r in result)
        
        delta_mu2 = [(self._mu2[i] - self._mu2[i-1])/2.0
                     for i in range(1, len(self._mu2))]
        delta_lambda = [(self._lambda[i] - self._lambda[i-1])/2.0
                        for i in range(1, len(self._lambda))]

        from sage.plot.colors import get_cmap
        cmap = get_cmap(cmap)
        def tile(i, j, value):
            m = self._mu2[i]
            if i == 0:
                dm = (delta_mu2[0], delta_mu2[0])
            elif i == len(delta_mu2):
                dm = (delta_mu2[-1], delta_mu2[-1])
            else:
                dm = (delta_mu2[i-1], delta_mu2[i])
            l = self._lambda[j]
            if j == 0:
                dl = (delta_lambda[0], delta_lambda[0])
            elif j == len(delta_lambda):
                dl = (delta_lambda[-1], delta_lambda[-1])
            else:
                dl = (delta_lambda[j-1], delta_lambda[j])
            norm_value = (value - value_min) / (value_max - value_min)
            print value, norm_value
            c = cmap(norm_value)[:3]
            v0 = (l-dl[0], m-dm[0])
            v1 = (l+dl[1], m-dm[0])
            v2 = (l+dl[1], m+dm[1])
            v3 = (l-dl[0], m+dm[1])
            return polygon2d([v0, v1, v2, v3], rgbcolor=c, fill=True)
        g = Graphics()
        g.axes_labels([r'$\lambda$', r'$\mu^2$'])
        g._set_extra_kwds({
            'title': self.title,
            'axes_labels': [r'$\lambda$', r'$\mu^2$'],
            'title_pos': (0.5, 1.1),
        })
        for i, j, value in result:
            g = tile(i, j, value) + g
        return g
Example #2
0
    def _render_on_subplot(self, subplot):
        """
        TESTS:

        A somewhat random plot, but fun to look at::

            sage: x,y = var('x,y')
            sage: density_plot(x^2-y^3+10*sin(x*y), (x, -4, 4), (y, -4, 4),plot_points=121,cmap='hsv')
        """
        options = self.options()
        cmap = get_cmap(options['cmap'])
        
        x0,x1 = float(self.xrange[0]), float(self.xrange[1])
        y0,y1 = float(self.yrange[0]), float(self.yrange[1])
        
        subplot.imshow(self.xy_data_array, origin='lower', cmap=cmap, extent=(x0,x1,y0,y1), interpolation=options['interpolation'])
Example #3
0
    def _render_on_subplot(self, subplot):
        """
        TESTS:

        A somewhat random plot, but fun to look at::

            sage: x,y = var('x,y')
            sage: contour_plot(x^2-y^3+10*sin(x*y), (x, -4, 4), (y, -4, 4),plot_points=121,cmap='hsv')
        """
        from sage.rings.integer import Integer
        options = self.options()
        fill = options['fill']
        contours = options['contours']
        if options.has_key('cmap'):
            cmap = get_cmap(options['cmap'])
        elif fill or contours is None:
            cmap = get_cmap('gray')
        else:
            if isinstance(contours, (int, Integer)):
                cmap = get_cmap([(i,i,i) for i in xsrange(0,1,1/contours)])
            else:
                l = Integer(len(contours))
                cmap = get_cmap([(i,i,i) for i in xsrange(0,1,1/l)])

        x0,x1 = float(self.xrange[0]), float(self.xrange[1])
        y0,y1 = float(self.yrange[0]), float(self.yrange[1])

        if isinstance(contours, (int, Integer)):
            contours = int(contours)

        CSF=None
        if fill:
            if contours is None:
                CSF=subplot.contourf(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1), label=options['legend_label'])
            else:
                CSF=subplot.contourf(self.xy_data_array, contours, cmap=cmap, extent=(x0,x1,y0,y1),extend='both', label=options['legend_label'])

        linewidths = options.get('linewidths',None)
        if isinstance(linewidths, (int, Integer)):
            linewidths = int(linewidths)
        elif isinstance(linewidths, (list, tuple)):
            linewidths = tuple(int(x) for x in linewidths)
        linestyles = options.get('linestyles',None)
        if contours is None:
            CS = subplot.contour(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1),
                                 linewidths=linewidths, linestyles=linestyles, label=options['legend_label'])
        else:
            CS = subplot.contour(self.xy_data_array, contours, cmap=cmap, extent=(x0,x1,y0,y1),
                            linewidths=linewidths, linestyles=linestyles, label=options['legend_label'])
        if options.get('labels', False):
            label_options = options['label_options']
            label_options['fontsize'] = int(label_options['fontsize'])
            if fill and label_options is None:
                label_options['inline']=False
            subplot.clabel(CS, **label_options)
        if options.get('colorbar', False):
            colorbar_options = options['colorbar_options']
            from matplotlib import colorbar
            cax,kwds=colorbar.make_axes_gridspec(subplot,**colorbar_options)
            if CSF is None:
                cb=colorbar.Colorbar(cax,CS, **kwds)
            else:
                cb=colorbar.Colorbar(cax,CSF, **kwds)
                cb.add_lines(CS)
Example #4
0
    def _render_on_subplot(self, subplot):
        """
        TESTS::

            sage: matrix_plot(random_matrix(RDF, 50), cmap='jet')
            Graphics object consisting of 1 graphics primitive
        """
        options = self.options()
        cmap = get_cmap(options.pop('cmap',None))
        origin=options['origin']

        norm=options['norm']

        if norm=='value':
            import matplotlib
            norm=matplotlib.colors.NoNorm()

        if options['subdivisions']:
            subdiv_options=options['subdivision_options']
            if isinstance(subdiv_options['boundaries'], (list, tuple)):
                rowsub,colsub=subdiv_options['boundaries']
            else:
                rowsub=subdiv_options['boundaries']
                colsub=subdiv_options['boundaries']
            if isinstance(subdiv_options['style'], (list, tuple)):
                rowstyle,colstyle=subdiv_options['style']
            else:
                rowstyle=subdiv_options['style']
                colstyle=subdiv_options['style']
            if rowstyle is None:
                rowstyle=dict()
            if colstyle is None:
                colstyle=dict()

            # Make line objects for subdivisions
            from .line import line2d
            lim=self.get_minmax_data()
            # First draw horizontal lines representing row subdivisions
            for y in rowsub:
                l=line2d([(lim['xmin'],y-0.5), (lim['xmax'],y-0.5)], **rowstyle)[0]
                l._render_on_subplot(subplot)
            for x in colsub:
                l=line2d([(x-0.5, lim['ymin']), (x-0.5, lim['ymax'])], **colstyle)[0]
                l._render_on_subplot(subplot)

        if hasattr(self.xy_data_array, 'tocoo'):
            # Sparse matrix -- use spy
            opts=options.copy()
            for opt in ['vmin', 'vmax', 'norm', 'origin','subdivisions','subdivision_options',
                        'colorbar','colorbar_options']:
                del opts[opt]
            if origin=='lower':
                subplot.spy(self.xy_data_array.tocsr()[::-1], **opts)
            else:
                subplot.spy(self.xy_data_array, **opts)
        else:
            opts = dict(cmap=cmap, interpolation='nearest', aspect='equal',
                      norm=norm, vmin=options['vmin'], vmax=options['vmax'],
                      origin=origin,zorder=options.get('zorder',None))
            image = subplot.imshow(self.xy_data_array, **opts)

            if options.get('colorbar', False):
                colorbar_options = options['colorbar_options']
                from matplotlib import colorbar
                cax,kwds=colorbar.make_axes_gridspec(subplot,**colorbar_options)
                colorbar.Colorbar(cax, image, **kwds)

        if origin == 'upper':
            subplot.xaxis.tick_top()
        elif origin == 'lower':
            subplot.xaxis.tick_bottom()
        subplot.xaxis.set_ticks_position('both') #only tick marks, not tick labels
Example #5
0
    def _render_on_subplot(self, subplot):
        """
        TESTS::

            sage: matrix_plot(random_matrix(RDF, 50), cmap='jet')
            Graphics object consisting of 1 graphics primitive
        """
        options = self.options()
        cmap = get_cmap(options.pop('cmap',None))
        origin=options['origin']

        norm=options['norm']

        if norm=='value':
            import matplotlib
            norm=matplotlib.colors.NoNorm()

        if options['subdivisions']:
            subdiv_options=options['subdivision_options']
            if isinstance(subdiv_options['boundaries'], (list, tuple)):
                rowsub,colsub=subdiv_options['boundaries']
            else:
                rowsub=subdiv_options['boundaries']
                colsub=subdiv_options['boundaries']
            if isinstance(subdiv_options['style'], (list, tuple)):
                rowstyle,colstyle=subdiv_options['style']
            else:
                rowstyle=subdiv_options['style']
                colstyle=subdiv_options['style']
            if rowstyle is None:
                rowstyle=dict()
            if colstyle is None:
                colstyle=dict()

            # Make line objects for subdivisions
            from line import line2d
            lim=self.get_minmax_data()
            # First draw horizontal lines representing row subdivisions
            for y in rowsub:
                l=line2d([(lim['xmin'],y-0.5), (lim['xmax'],y-0.5)], **rowstyle)[0]
                l._render_on_subplot(subplot)
            for x in colsub:
                l=line2d([(x-0.5, lim['ymin']), (x-0.5, lim['ymax'])], **colstyle)[0]
                l._render_on_subplot(subplot)

        if hasattr(self.xy_data_array, 'tocoo'):
            # Sparse matrix -- use spy
            opts=options.copy()
            for opt in ['vmin', 'vmax', 'norm', 'origin','subdivisions','subdivision_options',
                        'colorbar','colorbar_options']:
                del opts[opt]
            if origin=='lower':
                subplot.spy(self.xy_data_array.tocsr()[::-1], **opts)
            else:
                subplot.spy(self.xy_data_array, **opts)
        else:
            opts = dict(cmap=cmap, interpolation='nearest', aspect='equal',
                      norm=norm, vmin=options['vmin'], vmax=options['vmax'],
                      origin=origin,zorder=options.get('zorder',None))
            image=subplot.imshow(self.xy_data_array, **opts)

            if options.get('colorbar', False):
                colorbar_options = options['colorbar_options']
                from matplotlib import colorbar
                cax,kwds=colorbar.make_axes_gridspec(subplot,**colorbar_options)
                cb=colorbar.Colorbar(cax,image, **kwds)

        if origin=='upper':
            subplot.xaxis.tick_top()
        elif origin=='lower':
            subplot.xaxis.tick_bottom()
        subplot.xaxis.set_ticks_position('both') #only tick marks, not tick labels
    def _render_on_subplot(self, subplot):
        """
        TESTS:

        A somewhat random plot, but fun to look at::

            sage: x,y = var('x,y')
            sage: contour_plot(x^2-y^3+10*sin(x*y), (x, -4, 4), (y, -4, 4),plot_points=121,cmap='hsv')
        """
        from sage.rings.integer import Integer
        options = self.options()
        fill = options['fill']
        contours = options['contours']
        if 'cmap' in options:
            cmap = get_cmap(options['cmap'])
        elif fill or contours is None:
            cmap = get_cmap('gray')
        else:
            if isinstance(contours, (int, Integer)):
                cmap = get_cmap([(i, i, i)
                                 for i in xsrange(0, 1, 1 / contours)])
            else:
                l = Integer(len(contours))
                cmap = get_cmap([(i, i, i) for i in xsrange(0, 1, 1 / l)])

        x0, x1 = float(self.xrange[0]), float(self.xrange[1])
        y0, y1 = float(self.yrange[0]), float(self.yrange[1])

        if isinstance(contours, (int, Integer)):
            contours = int(contours)

        CSF = None
        if fill:
            if contours is None:
                CSF = subplot.contourf(self.xy_data_array,
                                       cmap=cmap,
                                       extent=(x0, x1, y0, y1),
                                       label=options['legend_label'])
            else:
                CSF = subplot.contourf(self.xy_data_array,
                                       contours,
                                       cmap=cmap,
                                       extent=(x0, x1, y0, y1),
                                       extend='both',
                                       label=options['legend_label'])

        linewidths = options.get('linewidths', None)
        if isinstance(linewidths, (int, Integer)):
            linewidths = int(linewidths)
        elif isinstance(linewidths, (list, tuple)):
            linewidths = tuple(int(x) for x in linewidths)

        from sage.plot.misc import get_matplotlib_linestyle
        linestyles = options.get('linestyles', None)
        if isinstance(linestyles, (list, tuple)):
            linestyles = [
                get_matplotlib_linestyle(l, 'long') for l in linestyles
            ]
        else:
            linestyles = get_matplotlib_linestyle(linestyles, 'long')
        if contours is None:
            CS = subplot.contour(self.xy_data_array,
                                 cmap=cmap,
                                 extent=(x0, x1, y0, y1),
                                 linewidths=linewidths,
                                 linestyles=linestyles,
                                 label=options['legend_label'])
        else:
            CS = subplot.contour(self.xy_data_array,
                                 contours,
                                 cmap=cmap,
                                 extent=(x0, x1, y0, y1),
                                 linewidths=linewidths,
                                 linestyles=linestyles,
                                 label=options['legend_label'])
        if options.get('labels', False):
            label_options = options['label_options']
            label_options['fontsize'] = int(label_options['fontsize'])
            if fill and label_options is None:
                label_options['inline'] = False
            subplot.clabel(CS, **label_options)
        if options.get('colorbar', False):
            colorbar_options = options['colorbar_options']
            from matplotlib import colorbar
            cax, kwds = colorbar.make_axes_gridspec(subplot,
                                                    **colorbar_options)
            if CSF is None:
                cb = colorbar.Colorbar(cax, CS, **kwds)
            else:
                cb = colorbar.Colorbar(cax, CSF, **kwds)
                cb.add_lines(CS)
Example #7
0
    def _render_on_subplot(self, subplot):
        """
        TESTS::

            sage: matrix_plot(random_matrix(RDF, 50), cmap='jet')
        """
        options = self.options()
        cmap = get_cmap(options.pop("cmap", None))
        origin = options["origin"]

        norm = options["norm"]

        if norm == "value":
            import matplotlib

            norm = matplotlib.colors.NoNorm()

        if options["subdivisions"]:
            subdiv_options = options["subdivision_options"]
            if isinstance(subdiv_options["boundaries"], (list, tuple)):
                rowsub, colsub = subdiv_options["boundaries"]
            else:
                rowsub = subdiv_options["boundaries"]
                colsub = subdiv_options["boundaries"]
            if isinstance(subdiv_options["style"], (list, tuple)):
                rowstyle, colstyle = subdiv_options["style"]
            else:
                rowstyle = subdiv_options["style"]
                colstyle = subdiv_options["style"]
            if rowstyle is None:
                rowstyle = dict()
            if colstyle is None:
                colstyle = dict()

            # Make line objects for subdivisions
            from line import line2d

            lim = self.get_minmax_data()
            # First draw horizontal lines representing row subdivisions
            for y in rowsub:
                l = line2d([(lim["xmin"], y - 0.5), (lim["xmax"], y - 0.5)], **rowstyle)[0]
                l._render_on_subplot(subplot)
            for x in colsub:
                l = line2d([(x - 0.5, lim["ymin"]), (x - 0.5, lim["ymax"])], **colstyle)[0]
                l._render_on_subplot(subplot)

        if hasattr(self.xy_data_array, "tocoo"):
            # Sparse matrix -- use spy
            opts = options.copy()
            for opt in [
                "vmin",
                "vmax",
                "norm",
                "origin",
                "subdivisions",
                "subdivision_options",
                "colorbar",
                "colorbar_options",
            ]:
                del opts[opt]
            if origin == "lower":
                subplot.spy(self.xy_data_array.tocsr()[::-1], **opts)
            else:
                subplot.spy(self.xy_data_array, **opts)
        else:
            opts = dict(
                cmap=cmap,
                interpolation="nearest",
                aspect="equal",
                norm=norm,
                vmin=options["vmin"],
                vmax=options["vmax"],
                origin=origin,
                zorder=options.get("zorder", None),
            )
            image = subplot.imshow(self.xy_data_array, **opts)

            if options.get("colorbar", False):
                colorbar_options = options["colorbar_options"]
                from matplotlib import colorbar

                cax, kwds = colorbar.make_axes_gridspec(subplot, **colorbar_options)
                cb = colorbar.Colorbar(cax, image, **kwds)

        if origin == "upper":
            subplot.xaxis.tick_top()
        elif origin == "lower":
            subplot.xaxis.tick_bottom()
        subplot.xaxis.set_ticks_position("both")  # only tick marks, not tick labels