0

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 enter image description here

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 :)

woot
  • 7,406
  • 2
  • 36
  • 55
ehmath
  • 119
  • 2
  • 8

1 Answers1

0

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:

example output

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:

enter image description here

PPS: As your list contains 0.21 twice, one rectangular has width zero.

wobsta
  • 721
  • 4
  • 5
  • Thanks for your effort but I think even in your code it does not draw what it must. Well, as I mentioned that the next rectangle must start where the last rectangle ends. And in your example all these boxes are are starting from the same begining point (X coordinate) I dont know if its a bug for pyX or not but it does not work with floating point numbers. With integer values, it works fine! – ehmath Feb 19 '15 at 08:58
  • Sure, the code does draw the rectangles side by side. If you use `i*0.1` for the starting value of the y coordinate you can clearly see it. I've added the result of this change to the answer. – wobsta Feb 20 '15 at 08:08