Beispiel #1
0
    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))
Beispiel #2
0
    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))
Beispiel #3
0
def rescale_profile_by_mass(profile, param, mass, radius):
    """
    Rescale a density profile by a total mass
    within some radius.

    Parameters
    ----------
    profile : Formula1D
        Formula that is a radial density profile.
    param : string
        The density-valued parameter that needs to be rescaled.
    mass : YTQuantity
        The mass of the object.
    radius : YTQuantity
        The radius that the *mass* corresponds to.

    Examples
    --------
    >>> import yt.units as u
    >>> import numpy as np
    >>> gas_density = AM06_density_profile()
    >>> a = 600.0*u.kpc
    >>> a_c = 60.0*u.kpc
    >>> c = 0.17
    >>> alpha = -2.0
    >>> beta = -3.0
    >>> M0 = 1.0e14*u.Msun
    >>> # Don't set the density parameter rho_0!
    >>> gas_density.set_param_values(a=a, a_c=a_c, c=c, 
    ...                              alpha=alpha, beta=beta)
    >>> rescale_profile_by_mass(gas_density, "rho_0", M0, np.inf*u.kpc)
    """
    R = float(in_cgs(radius).value)
    M = float(in_cgs(mass).value)
    rho = profile.copy()
    values = {}
    for n, p in profile.param_values.items():
        if n == param:
            # Set the density parameter to change to unity
            values[n] = 1.0
        elif p is None:
            raise RuntimeError("The parameter %s does not have a value!" % n)
        elif isinstance(p, float):
            values[n] = p
        else:
            values[n] = float(in_cgs(p).value)
    rho.set_param_values(**values)
    mass_int = lambda r: rho(r)*r*r
    scale = float(M/(4.*np.pi*quad(mass_int, [0, R])))
    u = get_units(mass/radius**3)
    quan = check_type(mass)(scale, "g/cm**3")
    profile.param_values[param] = in_units(quan, u)
Beispiel #4
0
def rescale_profile_by_mass(profile, param, mass, radius):
    """
    Rescale a density profile by a total mass
    within some radius.

    Parameters
    ----------
    profile : Formula1D
        Formula that is a radial density profile.
    param : string
        The density-valued parameter that needs to be rescaled.
    mass : YTQuantity
        The mass of the object.
    radius : YTQuantity
        The radius that the *mass* corresponds to.

    Examples
    --------
    >>> import yt.units as u
    >>> import numpy as np
    >>> gas_density = AM06_density_profile()
    >>> a = 600.0*u.kpc
    >>> a_c = 60.0*u.kpc
    >>> c = 0.17
    >>> alpha = -2.0
    >>> beta = -3.0
    >>> M0 = 1.0e14*u.Msun
    >>> # Don't set the density parameter rho_0!
    >>> gas_density.set_param_values(a=a, a_c=a_c, c=c, 
    ...                              alpha=alpha, beta=beta)
    >>> rescale_profile_by_mass(gas_density, "rho_0", M0, np.inf*u.kpc)
    """
    R = float(in_cgs(radius).value)
    M = float(in_cgs(mass).value)
    rho = profile.copy()
    values = {}
    for n, p in profile.param_values.items():
        if n == param:
            # Set the density parameter to change to unity
            values[n] = 1.0
        elif isinstance(p, float):
            values[n] = p
        else:
            values[n] = float(in_cgs(p).value)
    rho.set_param_values(**values)
    mass_int = lambda r: rho(r)*r*r
    scale = float(M/(4.*np.pi*quad(mass_int, [0, R])))
    u = get_units(mass/radius**3)
    quan = check_type(mass)(scale, "g/cm**3")
    profile.param_values[param] = in_units(quan, u)
Beispiel #5
0
    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))
Beispiel #6
0
    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))