开发者

python - how to put values in a word length histogram

开发者 https://www.devze.com 2023-04-11 05:06 出处:网络
i want to put values at y-axis in the histogr开发者_如何学Goam i made. I want every 100 values for example to put\"100-|\" , \"200-|\" etc.

i want to put values at y-axis in the histogr开发者_如何学Goam i made. I want every 100 values for example to put "100-|" , "200-|" etc. My code is:

def histogram(lenghts):
    xmax=max(lengths.keys())
    ymax=max(lenght.values())
    symbol=""
    indexing=""

    for j in range(ymax,-1,-10):
            symbol="{0}".format("|")
        for v in range(ymax,-1,-100):#here i try to put the values
            print("{0}{1:<4}".format(v,"-|"))

    #fill histogram
        for i in range(1,xmax):
            if i in lengths.keys() and lengths[i]>=j:
                symbol+="***"
            else:
                symbol+="   "
        print(symbol)

    #x-axis
    symbol="{:>5}".format("-+-")
    for i in range(1,xmax):
        symbol+="+--"
    print(symbol)

    #indexing x-axis
    for i in range(1,xmax):
        indexing+="{:>6}".format(i)
    print(indexing)

    return

I am getting values but only the same values ,for example "67-| , 167-| ,267-|". I can't figure how to do it right!


Here you have a working code. The trick is in the module operator that is used to draw the scale number of the y axis when the y axis is a number close to a hundred. There were some other minor problems in your code with the variable names.

def histogram(lenghts):
    xmax = max(lenghts.keys())
    ymax = max(lenghts.values())
    symbol = ""
    indexing = ""

    step = 10
    for j in range(ymax, -1, -step):
        if j % 100 < step:
            symbol = "{0:>3}{1:>3}".format(j, "-|")
        else:
            symbol = "{0:>3}{1:>3}".format(" ", "|")

        #fill histogram
        for i in range(1, xmax+1):
            if i in lenghts.keys() and lenghts[i] >= j:
                symbol += "***"
            else:
                symbol += "   "
        print(symbol)

    #x-axis
    symbol= "{0:>8}".format("-+--")
    for i in range(1, xmax+1):
        symbol += "+--"
    print(symbol)

    #indexing x-axis
    indexing = "    "
    for i in range(1, xmax+1):
        indexing += "{0:>3}".format(i)
    print(indexing)


lenghts = {4:104, 6:257, 10:157}
histogram(lenghts)

python - how to put values in a word length histogram

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号