1

I think I am making some trivial mistake here, but it has been baffling me for a while. I was trying to plot some lines using matplotlib but one of the lines was being ignored. Just to make sure that something else was not interfering with the line, I created a new script with just those three lines of plot but still I don't get the middle line (from (1,1) to (20,20)). Any help is appreciated, thanks in advance! I have reproduced the script and the output below:

import matplotlib.pyplot as plt
plt.plot([20,1], [1,20])
plt.plot([1,1], [20,20])
plt.plot([20,2], [2,20])

plt.xlim(0, 25)
plt.ylim(0, 25)
plt.gca().set_aspect('equal')   
plt.tight_layout()  
plt.show()

plot produced

2 Answers 2

1

You are moving from [1,1], it means move from point 1 to point 1 in x axis. So your horizontal distance is 0 because distance from 1 to 1 itself is 0. Also you have write [20,20], it means move from point 20 to point 20 in y axis. So your vertical distance is also 0 because distance from 20 to itself is 0, this is the reason you are not getting any line.

You will not get any line where your x1,x2 values are equal and y1,y2 values are also equal because your both horizontal and vertical distance will be 0. eg: [1,1][2,2], [10,10][15,15] and so on...

In first case you are getting line because of [20,1], [1,20]. Here using [20,1] you have moved from point 20 to point 1 in x axis so there is horizontal distance of 19. [1,20] moves from point 1 to point 20 in y axis. So your vertical distance is 19. so you are getting diagonal plot

Sign up to request clarification or add additional context in comments.

Comments

1

You don't get a line because the start and end points are the same. Remember that in order to get a line from (x1,y1) to (x2,y2) you have to write plt.plot([x1,x2],[y1,y2]). So I think what you want is plt.plot([1,20],[1,20])

So in total it would be:

import matplotlib.pyplot as plt
plt.plot([20,1], [1,20])
plt.plot([1,20], [1,20])
plt.plot([20,2], [2,20])

plt.xlim(0, 25)
plt.ylim(0, 25)
plt.gca().set_aspect('equal')   
plt.tight_layout()  
plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.