2017-04-22 51 views
1

我想從一組用戶輸入值繪製直方圖。Python:繪製直方圖向下

好吧,我在我的代碼更新到這一點:

number1 = "" 
number2 = "" 
number3 = "" 
number4 = "" 

numbers = input("Enter a string of positive integers separated by spaces: ") 
print(" ") 
newNum = numbers.split() 

line = 0 
col = 0 
lines = int(max(newNum)) 
length = len(newNum) 

while line<lines: 
    col = 0 
    while col<length: 
     if line<int(newNum[col]): 
      print('* ', end = '') 
     else: 
      print(' ') 
     col = col+1 
    line = line+1 
    print("") 

但是當我運行的代碼,我得到這樣的:

Enter a string of positive integers separated by spaces: 1 3 20 5 

* * * * 

* * * 

* * * 


* * 


* * 

什麼我現在缺讓我的直方圖打印這樣的?另外,它爲什麼不將值打印到20?

Enter a string of positive integers separated by spaces: 1 3 20 5 

* * * * 
    * * * 
    * * * 
    * * 
    * * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
+0

你嘗試過什麼?你卡在哪裏?請參閱[如何問](https://stackoverflow.com/questions/ask/advice?)。 –

+0

上面的代碼是我得到的。我只是不確定如何讓它翻轉。 –

+0

我不知道你的應用程序,但它會是一個選項使用scipy或matplotlib條形圖? – Kev1n91

回答

0

在你的例子中,你永遠不會重置col;你應該每次都設置col=0啓動第二while循環之前:

while line<lines: 
    col=0 
    while col<length: 
     ... 

否則內環一次只能運行。

嵌套循環for可能是更自然......

和行內打印時,你要沒有底線字符;您應該使用print('* ', end='')並在每行末尾使用空的print()

,那麼你應該儘快你的數字轉換成整數作爲possilbe:

newNum = [int(i) for i in numbers.split()] 

否則max(newNum)將返回'5' - 字符串中的最高位。

並且您仍然在print(' ')聲明中缺少並end=' '

這應該解決您的問題。


一個更優雅的版本 - 倒計時號碼列表,直到列表只由零組成:

def vertical_histo(number_list): 
    rest = number_list 
    while any(rest): 
     print(' '.join('*' if i else ' ' for i in rest)) 
     rest = [i-1 if i else 0 for i in rest] 

# to get your example, call this function with: 
vertical_histo(number_list=(1, 3, 20, 5))) 

rest持有*你仍然需要顯示爲每列

rest = (1, 3, 20, 5) # first round 
rest = [0, 0, 17, 2] # after 3 rounds 
# ...etc 
rest = [0, 0, 1, 0] # last round 
rest = [0, 0, 0, 0] # when done 

然後這些在每次迭代中向下移位(右)一個。

' '.join('*' if i else ' ' for i in ones) 

這則創建一個具有'*'字符串在rest每個條目不是零呢。否則會增加一個' '

any(rest)True只要ones包含非零條目。

0

你將不得不手動編程它。

例如,如果我們處於小於或低於元素的線上。 這種粗糙的樣品應引導你:

line = 0 
col = 0 
lines = max(newNum) 
length = len(newNum) 

return  
while line<lines: 
    while col<length: 
     if line<newNum[col]: 
      print "* ", 
     else: 
      print " ", 
     col = col+1 
    line = line+1 
    print "" 
0

感謝所有!有了這個代碼,我能夠解決的問題:

number1 = "" 
number2 = "" 
number3 = "" 
number4 = "" 

numbers = input("Enter a string of positive integers separated by spaces: ") 
print(" ") 
newNum = [int(i) for i in numbers.split()] 

line = 0 
col = 0 
lines = int(max(newNum)) 
length = len(newNum) 

while line<lines: 
    col = 0 
    while col<length: 
     if line<int(newNum[col]): 
      print('* ', end = '') 
     else: 
      print(' ', end = '') 
     col = col+1 
    line = line+1 
    print("") 
0

讓我們給名data包含每一列的深度在直方圖列表

data = [3, 5, 2] 

多少行,你要打印?

nrows = max(data) #=> 5 

如果你問Python來算你行,Python從0開始,並在nrows-1

for row in range(nrows): print(row) #=> 0 1 2 3 4 

所以該行沒有停止。 3的索引號row等於2和行號。 4具有指數3,現在想直方圖的第一列,你必須打印3 '+', 這意味着在連續三一'+'(即row=2),並沒有在列4號(即row=3

'+' if (data[col] > row) else ' ' 

這種分析後,我們可以寫一個計算直方圖

def histogram(data): 
    return '\n'.join(
     ''.join('+' if depth>row else ' ' for depth in data) 
     for row in range(max(data))) 

功能,我們準備測試它

In [36]: def histogram(data): 
    ...:   return '\n'.join(
    ...:    ''.join('+' if depth>row else ' ' for depth in data) 
    ...:    for row in range(max(data))) 
    ...: 

In [37]: print(histogram((3,4,5,6))) 
++++ 
++++ 
++++ 
+++ 
    ++ 
    + 

In [38]: 

或者更短,

In [38]: def histogram(d, nl='\n'.join, jn=''.join): 
...:  return nl(jn('+'if n>r else' 'for n in d)for r in range(max(d))) 
...: 

In [39]: print(histogram((3,4,5,6))) 
++++ 
++++ 
++++ 
+++ 
    ++ 
    + 

In [40]: