plot_container.py 文件源码

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

项目:yt 作者: yt-project 项目源码 文件源码
def _get_axes_unit_labels(self, unit_x, unit_y):
        axes_unit_labels = ['', '']
        comoving = False
        hinv = False
        for i, un in enumerate((unit_x, unit_y)):
            unn = None
            if hasattr(self.data_source, 'axis'):
                if hasattr(self.ds.coordinates, "image_units"):
                    # This *forces* an override
                    unn = self.ds.coordinates.image_units[
                        self.data_source.axis][i]
                elif hasattr(self.ds.coordinates, "default_unit_label"):
                    axax = getattr(self.ds.coordinates,
                                   "%s_axis" % ("xy"[i]))[self.data_source.axis]
                    unn = self.ds.coordinates.default_unit_label.get(
                        axax, None)
            if unn is not None:
                axes_unit_labels[i] = r'\ \ \left('+unn+r'\right)'
                continue
            # Use sympy to factor h out of the unit.  In this context 'un'
            # is a string, so we call the Unit constructor.
            expr = Unit(un, registry=self.ds.unit_registry).expr
            h_expr = Unit('h', registry=self.ds.unit_registry).expr
            # See http://docs.sympy.org/latest/modules/core.html#sympy.core.expr.Expr
            h_power = expr.as_coeff_exponent(h_expr)[1]
            # un is now the original unit, but with h factored out.
            un = str(expr*h_expr**(-1*h_power))
            un_unit = Unit(un, registry=self.ds.unit_registry)
            cm = Unit('cm').expr
            if str(un).endswith('cm') and cm not in un_unit.expr.atoms():
                comoving = True
                un = un[:-2]
            # no length units besides code_length end in h so this is safe
            if h_power == -1:
                hinv = True
            elif h_power != 0:
                # It doesn't make sense to scale a position by anything
                # other than h**-1
                raise RuntimeError
            if un not in ['1', 'u', 'unitary']:
                if un in formatted_length_unit_names:
                    un = formatted_length_unit_names[un]
                else:
                    un = Unit(un, registry=self.ds.unit_registry)
                    un = un.latex_representation()
                    if hinv:
                        un = un + '\,h^{-1}'
                    if comoving:
                        un = un + '\,(1+z)^{-1}'
                    pp = un[0]
                    if pp in latex_prefixes:
                        symbol_wo_prefix = un[1:]
                        if symbol_wo_prefix in prefixable_units:
                            un = un.replace(
                                pp, "{"+latex_prefixes[pp]+"}", 1)
                axes_unit_labels[i] = '\ \ ('+un+')'
        return axes_unit_labels
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号