I am using PyX and trying to create a series of rectangles such that every next rectangle must start where the last rectangle ends. The code goes like this:
//start of loop with index i
back_pos = dep_list[i-1]
front_pos = list[i]
c.stroke(path.rect(back_pos, 0, front_pos, 1))
Following are some of the values in list. Basically the list contains increasing order of numbers.
back_pos = 0.04 front_pos = 0.04
back_pos = 0.04 front_pos = 0.21
back_pos = 0.21 front_pos = 0.21
back_pos = 0.21 front_pos = 0.57
back_pos = 0.57 front_pos = 0.72
But when I execute it I get some thing like this following picture
Can someone please suggest me what am I doing wrong ? and how can I fix it ?
p.s I changed the Y-axis in that code so that the starting and ending point of the rectangles are visible. Thanks :)
You wrote dep_list
contains a list of increasing numbers, thus I guess it contains the end positions of the rectangles. In addition, note that the third parameter of rect
is the width of the rectangles, not the end position. A complete code could thus look like:
from pyx import *
dep_list = [0.04, 0.21, 0.21, 0.57, 0.72]
c = canvas.canvas()
back_pos = 0
for i in range(len(dep_list)):
if i == 0:
width = dep_list[i]
else:
width = dep_list[i] - dep_list[i-1]
c.stroke(path.rect(back_pos, 0, width, 1))
back_pos += width
c.writePDFfile()
resulting in:
There a plenty of options to do it differently. As a simple example you could set back_pos
to become dep_list[i]
at the last line in the loop. Anyway, I tried to write it in a very unfancy and explicit way. I hope that helps.
PS: You could use i*0.1
instead of 0
at the second argument of the rect
to get the rects shifted vertically. edit: The output becomes:
PPS: As your list contains 0.21
twice, one rectangular has width zero.