base.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:formulas 作者: jzuhone 项目源码 文件源码
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))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号