Search code examples
pythonmatplotlibplotline

matplotlib plot function ignores one line


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


Solution

  • 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