您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python函数相当于R的`pretty()`?

5b51 2022/1/14 8:22:10 python 字数 1963 阅读 514 来源 www.jb51.cc/python

我在 Python中复制一些R代码. 我惹恼了R的漂亮(). 我需要的只是漂亮(x),其中x是一些数字. 粗略地说,函数“计算漂亮的断点”作为几个“圆”值的序列.我不确定是否有Python等价物,而且我对Google没有太多运气. 编辑:更具体地说,这是漂亮的帮助页面中的描述条目: Description: Compute a sequence of about n+1 equally space

概述

我惹恼了R的漂亮().

我需要的只是漂亮(x),其中x是一些数字.

粗略地说,函数“计算漂亮的断点”作为几个“圆”值的序列.我不确定是否有Python等价物,而且我对Google没有太多运气.

编辑:更具体地说,这是漂亮的帮助页面中的描述条目:

Description: Compute a sequence of about n+1 equally spaced ‘round’ values which cover the range of the values in x. The values are chosen so that they are 1,2 or 5 times a power of 10.

我查看了R的pretty.default(),看看R究竟在做什么,但最终使用的是.Internal() – 这通常会导致黑暗R魔法.我以为在潜入之前我会问一下.

有谁知道Python是否有与R’s pretty()相同的东西?

import numpy as np

def nicenumber(x,round):
    exp = np.floor(np.log10(x))
    f   = x / 10**exp

    if round:
        if f < 1.5:
            nf = 1.
        elif f < 3.:
            nf = 2.
        elif f < 7.:
            nf = 5.
        else:
            nf = 10.
    else:
        if f <= 1.:
            nf = 1.
        elif f <= 2.:
            nf = 2.
        elif f <= 5.:
            nf = 5.
        else:
            nf = 10.

    return nf * 10.**exp

def pretty(low,high,n):
    range = nicenumber(high - low,False)
    d     = nicenumber(range / (n-1),True)
    miny  = np.floor(low  / d) * d
    maxy  = np.ceil (high / d) * d
    return np.arange(miny,maxy+0.5*d,d)

这产生了例如:

pretty(0.5,2.56,10)
pretty(0.5,25.6,256,10 )
pretty(0.5,2560,10)

[ 0.5 1. 1.5 2. 2.5 3. ]

[ 0. 5. 10. 15. 20. 25. 30.]

[ 0. 50. 100. 150. 200. 250. 300.]

[ 0. 500. 1000. 1500. 2000. 2500. 3000.]

总结

以上是编程之家为你收集整理的Python函数相当于R的`pretty()`?全部内容,希望文章能够帮你解决Python函数相当于R的`pretty()`?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶