LiamNeesonFan
LiamNeesonFan

Reputation: 2813

Connecting two points with a line in a plot in python

Basically what I have to do is draw a NxN grid within certain boundaries given by a boundingBox and write a function for that. My idea is that I should draw horizontal and vertical lines and calculating the width and height by dividing the range of the boundaries by N. Then I want to draw the lines with pyplot using the ends of the lines, and this is where I'm having trouble. I have something like this, but it's not well thought out. The boundingBox is basically coordinates.

def drawGridLines(boundingBox, N):
    lonrange = boundingBox[1][0] - boundingBox[0][0]
    latrange = boundingBox[1][1] - boundingBox[0][1]
    lonpieces = lonrange/N
    latpieces = latrange/N

    while (lonpieces <= N):
        lon = lonpieces
        pylab.plot(lon)
        lonpieces = lonpieces + (lonrange/N)

This is what I have

This is what I want to have now

Upvotes: 2

Views: 2027

Answers (1)

Matt
Matt

Reputation: 5684

I don't know too much about Python or Pylab, so I'm not sure I can fully answer your question, but I might be able to give some insight. It seems like you want each iteration of the loop to draw a line. It seems then like you may need two loops, not just one.

I would imagine your first loop may look something like:

//introduce some sort of counter variable
count =0
while(count <= N):
    //draw a vertical line at appropriate spot
    drawSpot = boundingBox[0][0] + count * lonPieces
    count = count +1

and your second loop may look like:

count =0
while(count <= N):
    //draw a horizontal line at appropriate spot
    drawSpot = boundingBox[0][1] + count * latPieces
    count = count +1

I may have horizontal and vertical mixed up, but I hope the pattern is clear. If you you have any questions , please leave a comment. Good luck!

Upvotes: 1

Related Questions