# Repetition in Python: Draw a Histogram

A histogram is a kind of graph. It usually represents the frequency of the oc­currence of certain discrete values. Common examples include temperature as a function of the month, or histograms of income as a function of year, age, race, or gender. Drawing one involves knowing how many categories there are and what the numerical values are for each category. Then the numbers are scaled so they fit in a particular area and the rectangles are drawn so that the heights reflect the relative numerical values. Figure 2.3 shows some typical examples.

A company wishes to plot a histogram of their income for each quarter of 2016. The numerical values are stored in variables Q1, Q2, Q3, and Q4, and range between 0 and 1 million. We can draw simple histograms by using text. If the histogram is drawn so that the bars are horizontal instead of vertical, then the number of characters drawn in a row can be used to represent the “height” of the histogram bar. Using the # character, a value of 20 could be drawn as follows:

Q1: #################### 20

This is another situation where a loop is necessary.

There are three parts to the histogram bar above: the label, the bar, and the data value. The label is easy to print, and in the example there are four possibili­ties; these are simply printed at the beginning of each line being drawn. The data value is not necessary, but it is useful for people looking at the graph to know what the exact number is. Each # character drawn could represent a range of values. The histogram bar is the trick. If numbers up to a million must be rep­resented, then the bar must be scaled so that it fits on a line. If 50 characters fit on a line, then each # printed needs to represent 1000000/50, or 20,000 dollars.

Another way to say this is that every \$20,000 of income results in one # character being printed. How many # are printed for the first quarter? Q1/20000 of them.

The print function prints out a line every time it is called. How can multiple things be printed on a line? The print statement has a special parameter to allow that. The call

print(i, end=’!’)

will print the variable i and then print the “! ” string following that, every time. Normally, the print statement places an end of line character (represented as “\n”) at the end of every line, but the end= clause allows the programmer to change this to whatever they like. If the string provided is empty (contains no characters), then nothing extra will be printed after each call, meaning specifi­cally that no end of line will be printed. Thus, the statement

print (“#”, end=””)

prints one # character, but no end of line. If another # is printed, then it will come right after the one just printed. This is exactly what is needed for the histo­gram program. A loop that prints ten # characters on one line can now be written as:

for i in range(0,10):

print (“#”, end=””)

Given that the value of the variable Q1 is between 0 and 1000000, and each 20000 should result in a single # character being printed, the first quarter histo­gram bar could be drawn by the following:

print (“Q1: “, end=””)

for k in range(0, int(Q1/20000)):

print (‘#’, end=”)

print (”      “,    q1)

This includes all of the labels, and the output looks like this:

Q1: ########     190000

A complete solution to the problem would draw the histogram all four quar­ters, along with a heading for the graph. The output might look like this:  Exercise 5 at the end of the chapter involves finishing this program.

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.