Ejemplo n.º 1
0
 def _repr_html_(self):
     ret = ''
     for k, v in self.plots.iteritems():
         canvas = FigureCanvasAgg(v)
         f = StringIO()
         canvas.print_figure(f)
         f.seek(0)
         img = base64.b64encode(f.read())
         ret += '<img src="data:image/png;base64,%s"><br>' % img
     return ret
Ejemplo n.º 2
0
 def _repr_html_(self):
     ret = ''
     for k, v in self.plots.items():
         canvas = FigureCanvasAgg(v)
         f = IO()
         canvas.print_figure(f)
         f.seek(0)
         img = base64.b64encode(f.read()).decode()
         ret += r'<img style="max-width:100%%;max-height:100%%;" ' \
                r'src="data:image/png;base64,%s"><br>' % img
     return ret
Ejemplo n.º 3
0
 def _repr_html_(self):
     ret = ''
     for k, v in self.plots.items():
         canvas = FigureCanvasAgg(v)
         f = IO()
         canvas.print_figure(f)
         f.seek(0)
         img = base64.b64encode(f.read()).decode()
         ret += r'<img style="max-width:100%%;max-height:100%%;" ' \
                r'src="data:image/png;base64,%s"><br>' % img
     return ret
Ejemplo n.º 4
0
Archivo: misc.py Proyecto: cevans216/yt
    def _repr_html_(self):
        from yt.visualization._mpl_imports import FigureCanvasAgg

        ret = ""
        for k, v in self.plots.items():
            canvas = FigureCanvasAgg(v)
            f = BytesIO()
            canvas.print_figure(f)
            f.seek(0)
            img = base64.b64encode(f.read()).decode()
            ret += (r'<img style="max-width:100%%;max-height:100%%;" '
                    r'src="data:image/png;base64,%s"><br>' % img)
        return ret
    def plot(self, fn=None, profile_field=None, profile_weight=None):
        """
        Save the current transfer function to a bitmap, or display
        it inline.

        Parameters
        ----------
        fn: string, optional
            Filename to save the image to. If None, the returns an image
            to an IPython session.

        Returns
        -------

        If fn is None, will return an image to an IPython notebook.

        """
        from yt.visualization._mpl_imports import FigureCanvasAgg
        from matplotlib.figure import Figure
        if self.tf is None:
            self.build_transfer_function()
            self.setup_default()
        tf = self.tf
        if self.log:
            xfunc = np.logspace
            xmi, xma = np.log10(self.bounds[0]), np.log10(self.bounds[1])
        else:
            xfunc = np.linspace
            # Need to strip units off of the bounds to avoid a recursion error
            # in matplotlib 1.3.1
            xmi, xma = [np.float64(b) for b in self.bounds]

        x = xfunc(xmi, xma, tf.nbins)
        y = tf.funcs[3].y
        w = np.append(x[1:] - x[:-1], x[-1] - x[-2])
        colors = np.array(
            [tf.funcs[0].y, tf.funcs[1].y, tf.funcs[2].y,
             np.ones_like(x)]).T

        fig = Figure(figsize=[6, 3])
        canvas = FigureCanvasAgg(fig)
        ax = fig.add_axes([0.2, 0.2, 0.75, 0.75])
        ax.bar(x,
               tf.funcs[3].y,
               w,
               edgecolor=[0.0, 0.0, 0.0, 0.0],
               log=self.log,
               color=colors,
               bottom=[0])

        if profile_field is not None:
            try:
                prof = self.profiles[self.field]
            except KeyError:
                self.setup_profile(profile_field, profile_weight)
                prof = self.profiles[self.field]
            try:
                prof[profile_field]
            except KeyError:
                prof.add_fields([profile_field])
            # Strip units, if any, for matplotlib 1.3.1
            xplot = np.array(prof.x)
            yplot = np.array(prof[profile_field] * tf.funcs[3].y.max() /
                             prof[profile_field].max())
            ax.plot(xplot, yplot, color='w', linewidth=3)
            ax.plot(xplot, yplot, color='k')

        ax.set_xscale({True: 'log', False: 'linear'}[self.log])
        ax.set_xlim(x.min(), x.max())
        ax.set_xlabel(self.ds._get_field_info(self.field).get_label())
        ax.set_ylabel(r'$\mathrm{alpha}$')
        ax.set_ylim(y.max() * 1.0e-3, y.max() * 2)

        if fn is None:
            from IPython.core.display import Image
            f = BytesIO()
            canvas.print_figure(f)
            f.seek(0)
            img = f.read()
            return Image(img)
        else:
            fig.savefig(fn)
    def plot(self, fn=None, profile_field=None, profile_weight=None):
        """
        Save the current transfer function to a bitmap, or display
        it inline.

        Parameters
        ----------
        fn: string, optional
            Filename to save the image to. If None, the returns an image
            to an IPython session.

        Returns
        -------

        If fn is None, will return an image to an IPython notebook.

        """
        if self.tf is None:
            self.build_transfer_function()
        tf = self.tf
        if self.log:
            xfunc = np.logspace
            xmi, xma = np.log10(self.bounds[0]), np.log10(self.bounds[1])
        else:
            xfunc = np.linspace
            # Need to strip units off of the bounds to avoid a recursion error
            # in matplotlib 1.3.1
            xmi, xma = [np.float64(b) for b in self.bounds]

        x = xfunc(xmi, xma, tf.nbins)
        y = tf.funcs[3].y
        w = np.append(x[1:] - x[:-1], x[-1] - x[-2])
        colors = np.array([tf.funcs[0].y, tf.funcs[1].y, tf.funcs[2].y, np.ones_like(x)]).T

        fig = Figure(figsize=[6, 3])
        canvas = FigureCanvasAgg(fig)
        ax = fig.add_axes([0.2, 0.2, 0.75, 0.75])
        ax.bar(x, tf.funcs[3].y, w, edgecolor=[0.0, 0.0, 0.0, 0.0], log=self.log, color=colors, bottom=[0])

        if profile_field is not None:
            try:
                prof = self.profiles[self.field]
            except KeyError:
                self.setup_profile(profile_field, profile_weight)
                prof = self.profiles[self.field]
            if profile_field not in prof.keys():
                prof.add_fields([profile_field], fractional=False, weight=profile_weight)
            # Strip units, if any, for matplotlib 1.3.1
            xplot = np.array(prof[self.field])
            yplot = np.array(prof[profile_field] * tf.funcs[3].y.max() / prof[profile_field].max())
            ax.plot(xplot, yplot, color="w", linewidth=3)
            ax.plot(xplot, yplot, color="k")

        ax.set_xscale({True: "log", False: "linear"}[self.log])
        ax.set_xlim(x.min(), x.max())
        ax.set_xlabel(self.ds._get_field_info(self.field).get_label())
        ax.set_ylabel(r"$\mathrm{alpha}$")
        ax.set_ylim(y.max() * 1.0e-3, y.max() * 2)

        if fn is None:
            from IPython.core.display import Image

            f = StringIO()
            canvas.print_figure(f)
            f.seek(0)
            img = f.read()
            return Image(img)
        else:
            fig.savefig(fn)