python中的直方图

我想画一个柱状图2 - 11和每个酒吧的数量将达到这一数字的链长。我有一个冰雹序列如下考虑

选择 | 换行 | 行号
  1. import string
  2. # from graphics import *
  3. from graphics import *
  4.  
  5. #Constant
  6. MAX = 10000
  7.  
  8. # printing functions
  9.  
  10. # this function prints a brief description of the program to the user
  11. # Inputs: none
  12. # Outputs : none
  13. def printGreeting():
  14.     print ""
  15.     print " This program finds hailstones sequences of numbers"
  16.     print " you choose. The next number in the sequence if found by"
  17.     print " either dividing it by two(if even) or multiplying by 3"
  18.     print " and adding 1(if odd).The program quits when the last"
  19.     print " number in the sequence is 1\n"
  20.  
  21. # this functions prints the menu for the user 
  22. # Inputs: none
  23. # Outputs : none
  24. def printMenu():
  25.     print "\n\tHere are your menu choices:"
  26.     print "\n\tI - view squence for an individual value\n"
  27.     print "\tR - veiw sequence for range of values\n"
  28.     print "\tL - Find the longest chain\n"
  29.     print "\tH - Print out the histogram\n"
  30.     print "\tQ - Quit\n\n"
  31.  
  32. # end of printing funtions
  33.  
  34.  
  35. # hailstone(number) prints the hailstone sequence
  36. # Inputs: number
  37. # Outputs: none
  38. def hailstone(n):
  39.  
  40.     length = 1
  41.     print n,
  42.     # checks if n is not sential 
  43.     while n != 1:
  44.  
  45.         # if even, divide by 2 (rule). add 1 to length
  46.         if n % 2 == 0:
  47.             n = n / 2
  48.             print "->",n,
  49.             length = length + 1
  50.  
  51.         # if odd, multiply by 3, add 1 (rule). add 1 to length
  52.         elif n % 2 != 0:
  53.             n = ( 3 * n ) + 1
  54.             print "->",n,
  55.             length = length + 1
  56.  
  57.     # print the length at the end of each chain
  58.     print "; length =",length
  59.     print "----------------------------------------------------------------",
  60.     print "--------------\n"
  61.     return length
  62.  
  63. # this function returns the length of each chain from the starting number, n
  64. # Inputs : n
  65. # Outputs : none
  66. def chain(n):
  67.  
  68.     length = 1
  69.     while n != 1:
  70.  
  71.         # if even, divide by 2 (rule). add 1 to length
  72.         if n % 2 == 0:
  73.             n = n / 2
  74.             length  = length + 1
  75.  
  76.         # if even, divide by 2 (rule). add 1 to length
  77.         elif n % 2 != 0:
  78.             n = ( 3 * n ) + 1
  79.             length = length + 1
  80.     return length
  81.  
  82. # getValidInt() prompts the user to enter an integer in the specified range,
  83. # rejects values not in that range by requiring new input, and only returns
  84. # a valid integer.
  85. # Inputs: the question of the prompt,
  86. #         the minimum value in the range
  87. #         and the maximum value in the range
  88. # Output: an integer in the specified range
  89. def getValidInt(question, min, max):
  90.  
  91.     # use a bad value to enter the loop
  92.     value = max + 1
  93.  
  94.     # compose the prompt 
  95.     prompt = question + " (" + str(min) + "-" + str(max) + "): "
  96.  
  97.     # continue to get values until the user enters a valid one
  98.     while value == "" or value < min or value > max:
  99.         value = raw_input(prompt)
  100.         if len(value) != 0:
  101.             value = int(value)
  102.  
  103.     # return a valid value
  104.     return value
  105.  
  106.  
  107. # this function finds the longest chain
  108. # Inputs: none
  109. # Outputs : none
  110. def longChain():
  111.      begin = "Please enter the begining integer for the range"
  112.      end = "Please enter the ending integer for the range"
  113.  
  114.      # calls to getValidInt for starting and ending values
  115.      beginNum = getValidInt(begin, 1, MAX)
  116.      endNum = getValidInt(end, beginNum + 1, MAX)
  117.  
  118.      largestChain = beginNum
  119.  
  120.      for i in range(beginNum, endNum+1):
  121.          if largestChain <= chain(i):
  122.              largestChain = i
  123.              length = chain(i)
  124.  
  125.      print largestChain, " had the longest chain ", length
  126.  
  127. # this function finds the longest chain***************************8
  128. # Inputs: none*************
  129. # Outputs : none    
  130. def histogram():
  131.     # initialize variables  
  132.     longestLength = 1
  133.     list = []
  134.  
  135.     start = input("Please enter the begining integer for the range: ")
  136.     for n in range (start, start + 10):
  137.         length = chain(n)
  138.         list.append(length)
  139.         if longestLength <= chain(n):
  140.             longestLength = n
  141.             length = chain(n)
  142.         print longestLength
  143.         print list
  144.  
  145. def main():
  146.  
  147.     # prints the greeting to the user
  148.     printGreeting()
  149.  
  150.     # prints the menu to the user
  151.     printMenu()
  152.  
  153.     # asks user the menu choice
  154.     choice = raw_input("Please enter your choice : ").upper()
  155.  
  156.     # checks to see if choice entered is from the menu
  157.     while choice != 'Q': 
  158.  
  159.         # if choice is "I" or "i", proceeds to follow the individual steps
  160.         if choice == 'I':
  161.             n = input("Please enter your integer (1-10000):")
  162.             hailstone(n)
  163.  
  164.         # if choice is "R" or "r", proceds print each number and its sequence
  165.         # until the last number is 1, uses getValidInt to get valid integers
  166.         # for the function
  167.         elif choice == 'R':
  168.  
  169.             begin = "Please enter the begining integer for the range"
  170.             end = "Please enter the ending integer for the range"
  171.  
  172.             # calls to getValidInt for starting and ending values
  173.             beginNum = getValidInt(begin, 1, MAX)
  174.             endNum = getValidInt(end, beginNum + 1, MAX)
  175.  
  176.             # for loop to get the values between starting and ending value
  177.             for n in range(beginNum,endNum+1):
  178.  
  179.                 #call to the hailstone function
  180.                 hailstone(n)
  181.  
  182.         # if choice is "L" or "l", proceeds to use getValidInt again to get the
  183.         # range, error checks on the range, and then calls the function chain
  184.         # to determine the length. 
  185.         elif choice == 'L':
  186.             # call to function longchain
  187.             longChain()
  188.  
  189.         elif choice == 'H':
  190.             histogram()
  191.  
  192.         # if niether of the menu choices, then it prints that the
  193.         # entered text is not a valid choices
  194.         else:
  195.             print choice, "is not a valid choice"
  196.  
  197.         # prints the menu to the user
  198.         printMenu()
  199.  
  200.         # asks user the menu choice
  201.         choice = raw_input("Please enter your choice : ").upper()
  202.  
  203. main()

但是我怎么能画一个柱状图2 - 11和每个酒吧的数量将达到这一数字的链长度在这个冰雹序列。

# 回答1

使用matplotlib可能是最好的办法图形包括直方图。
# 回答2

计算每个数字的链2到11并保存在一个字典。在Tkinter,您可以创建一个画布的X和Y轴分别在底部和离开。X轴将被标记为2到11和Y轴将标签匹配最长的碳链的高度。为每个2号通过创建矩形11显示适当的宽度和高度与链长(从字典获得)乘以一个因素适当的显示。
# 回答3

你可以这么做,但matplotlib图是一个很好的方法。它是用于出版物和非常灵活。这里有一个链接柱状图演示.有另一种可能性是使用gnuplot, gnuplot。py从你的计划来控制它。
# 回答4

我们希望允许用户画出直方图对多个范围的数字在运行的程序,我们需要关闭窗口后足够的时间把它。你应该关闭窗口后10秒。你需要使用睡眠()。所有图形窗口的处理应该在drawHistogram()函数。事实上,自窗口打开这个函数,这个函数是当地的。试图关闭窗口在main()会导致运行时错误。http://www.cs.umbc.edu/courses/201/f...ges/histo2.jpg
# 回答5

Glenton——我肯定你是对的,但是我想我会怎么做轮廓在Tkinter。我知道matplotlib但从来没有使用它。asong84——如果使用Tkinter,我建议使用小部件的方法后()而不是time . sleep ().
# 回答6

直方图是矩形,通常用不同的填充颜色。下面的代码没有意义,至少对我来说,所以看一下评论。

选择 | 换行 | 行号
  1. def histogram():
  2.      # initialize variables  
  3.      longestLength = 1
  4.      ## list is already used by python to convert something to 
  5.      ## a list, so always use another name
  6.      list = []           
  7.      start = input("Please enter the begining integer for the range: ")
  8.      for n in range (start, start + 10):
  9.  
  10.          ## You call the same chain(n) 3 times in this function
  11.          ## which doesn't make much sense.  Use "length" instead
  12.          length = chain(n)
  13.          list.append(length)
  14.          if longestLength <= chain(n): # chain(n)=length so longestLength <= length
  15.              longestLength = n
  16.              length = chain(n)    ## you already did this
  17.          print longestLength
  18.          print list 
# 回答7

dwblas我已经改变了,我现在想弄清楚如何让chainlength n的柱状图。matplotlib Glenton——我仍然尝试。会回到你如果我有一个问题。

标签: python

添加新评论