StudentOfScience
StudentOfScience

Reputation: 809

Script to draw pairs of numbers linearly in python

Say one has a list of 3 pairs of numbers:

[(100,200), (110, 190), (90, 210)]

I would like to write a script to automatically (for this set or any set of numbers) to draw such lines; the x refers to relative positions of numbers in each set and say the maximum length is 50 (i.e., sum of all - and x in each line); the point is to be to scale.

----x-----------------x----

-----x---------------x-----

---x-------------------x---

any help would be much appreciated!

Upvotes: 0

Views: 78

Answers (1)

tamasgal
tamasgal

Reputation: 26259

This one is pretty close to what you're looking for:

number_pairs = [(100,200), (110, 190), (90, 210)]

max_width = 50
min_value = min([i for j in number_pairs for i in j])
max_value = max([i for j in number_pairs for i in j])
step = max_width / float(max_value - min_value)

for num1, num2 in number_pairs:
    line = list('-' * max_width)
    pos1 = int((num1 - min_value + 1) * step - 1)
    pos2 = int((num2 - min_value + 1) * step - 1)
    line[pos1] = 'x'
    line[pos2] = 'x'
    print(''.join(line))

Upvotes: 1

Related Questions