예제 #1
0
파일: base.py 프로젝트: jzuhone/formulas
    def quick_plot(self, x_min, x_max, x_scale="linear", y_scale="linear",
                   res=200, filename=None, function_name="f", units=None): # pragma: no cover
        """
        Plot the formula.

        Parameters
        ----------
        x_min : unitful scalar quantity
            The mininum value of the "x" variable to plot.
        x_max : unitful scalar quantity
            The maximum value of the "x" variable to plot.
        x_scale : string
            The scaling for the x axis. Can be "linear" or "log".
        y_scale : string
            The scaling for the y axis. Can be "linear" or "log".
        res : integer
            The number of points to use in the plot. Default is 200.
        filename : str
            If set, save the plot to this filename.
        function_name : str
            The name of the function for the y-axis of the plot.
        units : str
            The units to convert the y-axis values to.

        Examples
        --------
        >>> import astropy.units as u
        >>> r_min = 0.1*u.kpc
        >>> r_max = 1000.*u.kpc
        >>> density_profile.quick_plot(r_min, r_max, x_scale="log")
        """
        from IPython.display import HTML, display
        matplotlib.rc("font", size=16, family="serif")
        fig = matplotlib.figure.Figure(figsize=(8,8))
        ax = fig.add_subplot(111)
        arr = check_type(x_min)
        x = arr(np.linspace(x_min.value, x_max.value, num=res), get_units(x_min))
        y = arr(self(x))
        if units is not None:
            y = in_units(y, units)
        x_units = latexify_units(x)
        y_units = latexify_units(y)
        ax.plot(np.array(x), np.array(y))
        ax.set_xlabel(r"$\mathrm{%s}$ (" % self.x + x_units + ")")
        ax.set_ylabel(r"$\mathrm{%s(%s)}$ (" % (function_name, self.x) + y_units + ")")
        ax.set_xscale(x_scale)
        ax.set_yscale(y_scale)
        fig.tight_layout()
        if filename is not None:
            fig.savefig(filename)
        canvas = FigureCanvasAgg(fig)
        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,{0}"><br>'.format(img)
        display(HTML(ret))
예제 #2
0
파일: base.py 프로젝트: jzuhone/formulas
    def quick_plot(self, x_min, x_max, x_scale="linear", y_scale="linear",
                   res=200, filename=None, function_name="f", units=None): # pragma: no cover
        """
        Plot the formula.

        Parameters
        ----------
        x_min : unitful scalar quantity
            The mininum value of the "x" variable to plot.
        x_max : unitful scalar quantity
            The maximum value of the "x" variable to plot.
        x_scale : string
            The scaling for the x axis. Can be "linear" or "log".
        y_scale : string
            The scaling for the y axis. Can be "linear" or "log".
        res : integer
            The number of points to use in the plot. Default is 200.
        filename : str
            If set, save the plot to this filename.
        function_name : str
            The name of the function for the y-axis of the plot.
        units : str
            The units to convert the y-axis values to.

        Examples
        --------
        >>> import astropy.units as u
        >>> r_min = 0.1*u.kpc
        >>> r_max = 1000.*u.kpc
        >>> density_profile.quick_plot(r_min, r_max, x_scale="log")
        """
        from IPython.display import HTML, display
        matplotlib.rc("font", size=16, family="serif")
        fig = matplotlib.figure.Figure(figsize=(8,8))
        ax = fig.add_subplot(111)
        arr = check_type(x_min)
        x = arr(np.linspace(x_min.value, x_max.value, num=res), get_units(x_min))
        y = arr(self(x))
        if units is not None:
            y = in_units(y, units)
        x_units = latexify_units(x)
        y_units = latexify_units(y)
        ax.plot(np.array(x), np.array(y))
        ax.set_xlabel(r"$\mathrm{%s}$ (" % self.x + x_units + ")")
        ax.set_ylabel(r"$\mathrm{%s(%s)}$ (" % (function_name, self.x) + y_units + ")")
        ax.set_xscale(x_scale)
        ax.set_yscale(y_scale)
        fig.tight_layout()
        if filename is not None:
            fig.savefig(filename)
        canvas = FigureCanvasAgg(fig)
        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,{0}"><br>'.format(img)
        display(HTML(ret))
예제 #3
0
파일: base.py 프로젝트: jzuhone/formulas
    def quick_plot(self, x_min, x_max, y_min, y_max, x_scale="linear",
                   y_scale="linear", z_scale="linear", res_x=200,
                   res_y=200, filename=None, cmap=None, function_name="f",
                   units=None): # pragma: no cover
        """
        Plot the formula.

        Parameters
        ----------
        x_min : unitful scalar quantity
            The mininum value of the "x" variable to plot.
        x_max : unitful scalar quantity
            The maximum value of the "x" variable to plot.
        y_min : unitful scalar quantity
            The mininum value of the "y" variable to plot.
        y_max : unitful scalar quantity
            The maximum value of the "y" variable to plot.
        x_scale : string
            The scaling for the x axis. Can be "linear" or "log".
        y_scale : string
            The scaling for the y axis. Can be "linear" or "log".
        z_scale : string
            The scaling for the z axis. Can be "linear" or "log".
        res_x : integer
            The number of points to use in the plot along the x-axis.
            Default is 200.
        res_y : integer
            The number of points to use in the plot along the y-axis.
            Default is 200.
        filename : str
            If set, save the plot to this filename.
        cmap : str
            The colormap to use when making the plot.
        function_name : str
            The name of the function for the colormap.
        units : str
            The units to convert the colormap values to.

        Examples
        --------
        >>> import yt.units as u
        >>> g_x = gaussian(x="v_x", A="A_x", mu="mu_x", sigma="sigma_x")
        >>> g_y = gaussian(x="v_y", A="A_y", mu="mu_y", sigma="sigma_y")
        >>> g = g_x*g_y
        >>> g.set_param_values(A_x=1.0, A_y=1.0, mu_x=0*u.km/u.s, mu_y=0*u.km/u.s,
        ...                    sigma_x=200*u.km/u.s, sigma_y=100*u.km/u.s)
        >>> g.quick_plot(-300*u.km/u.s, 300*u.km/u.s, -300*u.km/u.s, 300*u.km/u.s,)
        """
        from IPython.display import display, HTML
        from mpl_toolkits.axes_grid1 import make_axes_locatable
        matplotlib.rc("font", size=16, family="serif")
        fig = matplotlib.figure.Figure(figsize=(8,8))
        ax = fig.add_subplot(111)
        arr = check_type(x_min)
        x = np.linspace(x_min.value, x_max.value, num=res_x)
        y = np.linspace(y_min.value, y_max.value, num=res_y)
        xx, yy = np.meshgrid(x, y)
        xx = arr(xx, get_units(x_min))
        yy = arr(yy, get_units(y_min))
        vars = {str(self.x):xx,str(self.y):yy}
        z = arr(self(**vars))
        if units is not None:
            z = in_units(z, units)
        x_units = latexify_units(xx)
        y_units = latexify_units(yy)
        z_units = latexify_units(z)
        extent = (x[0], x[-1], y[0], y[-1])
        im = ax.imshow(z, extent=extent, cmap=cmap)
        ax.set_xlabel(r"$\mathrm{%s}$ (" % self.x + x_units + ")")
        ax.set_ylabel(r"$\mathrm{%s}$ (" % self.y + y_units + ")")
        ax.set_xscale(x_scale)
        ax.set_yscale(y_scale)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cbar = fig.colorbar(im, cax=cax)
        cbar.set_label(r"$\mathrm{%s(%s,%s)}$ (" % (function_name, self.x, self.y) + z_units + ")")
        cbar.ax.set_yscale(z_scale)
        fig.tight_layout()
        if filename is not None:
            fig.savefig(filename)
        canvas = FigureCanvasAgg(fig)
        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,{0}"><br>'.format(img)
        display(HTML(ret))
예제 #4
0
파일: base.py 프로젝트: jzuhone/formulas
    def quick_plot(self, x_min, x_max, y_min, y_max, x_scale="linear",
                   y_scale="linear", z_scale="linear", res_x=200,
                   res_y=200, filename=None, cmap=None, function_name="f",
                   units=None): # pragma: no cover
        """
        Plot the formula.

        Parameters
        ----------
        x_min : unitful scalar quantity
            The mininum value of the "x" variable to plot.
        x_max : unitful scalar quantity
            The maximum value of the "x" variable to plot.
        y_min : unitful scalar quantity
            The mininum value of the "y" variable to plot.
        y_max : unitful scalar quantity
            The maximum value of the "y" variable to plot.
        x_scale : string
            The scaling for the x axis. Can be "linear" or "log".
        y_scale : string
            The scaling for the y axis. Can be "linear" or "log".
        z_scale : string
            The scaling for the z axis. Can be "linear" or "log".
        res_x : integer
            The number of points to use in the plot along the x-axis.
            Default is 200.
        res_y : integer
            The number of points to use in the plot along the y-axis.
            Default is 200.
        filename : str
            If set, save the plot to this filename.
        cmap : str
            The colormap to use when making the plot.
        function_name : str
            The name of the function for the colormap.
        units : str
            The units to convert the colormap values to.

        Examples
        --------
        >>> import yt.units as u
        >>> g_x = gaussian(x="v_x", A="A_x", mu="mu_x", sigma="sigma_x")
        >>> g_y = gaussian(x="v_y", A="A_y", mu="mu_y", sigma="sigma_y")
        >>> g = g_x*g_y
        >>> g.set_param_values(A_x=1.0, A_y=1.0, mu_x=0*u.km/u.s, mu_y=0*u.km/u.s,
        ...                    sigma_x=200*u.km/u.s, sigma_y=100*u.km/u.s)
        >>> g.quick_plot(-300*u.km/u.s, 300*u.km/u.s, -300*u.km/u.s, 300*u.km/u.s,)
        """
        from IPython.display import display, HTML
        from mpl_toolkits.axes_grid1 import make_axes_locatable
        matplotlib.rc("font", size=16, family="serif")
        fig = matplotlib.figure.Figure(figsize=(8,8))
        ax = fig.add_subplot(111)
        arr = check_type(x_min)
        x = np.linspace(x_min.value, x_max.value, num=res_x)
        y = np.linspace(y_min.value, y_max.value, num=res_y)
        xx, yy = np.meshgrid(x, y)
        xx = arr(xx, get_units(x_min))
        yy = arr(yy, get_units(y_min))
        vars = {str(self.x):xx,str(self.y):yy}
        z = arr(self(**vars))
        if units is not None:
            z = in_units(z, units)
        x_units = latexify_units(xx)
        y_units = latexify_units(yy)
        z_units = latexify_units(z)
        extent = (x[0], x[-1], y[0], y[-1])
        im = ax.imshow(z, extent=extent, cmap=cmap)
        ax.set_xlabel(r"$\mathrm{%s}$ (" % self.x + x_units + ")")
        ax.set_ylabel(r"$\mathrm{%s}$ (" % self.y + y_units + ")")
        ax.set_xscale(x_scale)
        ax.set_yscale(y_scale)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cbar = fig.colorbar(im, cax=cax)
        cbar.set_label(r"$\mathrm{%s(%s,%s)}$ (" % (function_name, self.x, self.y) + z_units + ")")
        cbar.ax.set_yscale(z_scale)
        fig.tight_layout()
        if filename is not None:
            fig.savefig(filename)
        canvas = FigureCanvasAgg(fig)
        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,{0}"><br>'.format(img)
        display(HTML(ret))