M.Faheem Sajjad
M.Faheem Sajjad

Reputation: 31

vertical and horizontal lines draw through plt.plot() method --Global API--Matplotlib

I am practicing basic matplotlib plotting, but can't understand these axis for vertical and horizontal lines.

plt.plot([x, y, z], [x, y, z])

Actual example,

plt.figure(figsize=(12, 6))
plt.title('My Nice Plot')

plt.subplot(1, 2, 1)  # rows, columns, panel selected
plt.plot(x, x ** 2)
plt.plot([0, 0, 0], [-10, 0, 100])  <<<<<<======= ???????
plt.legend(['X^2', 'Vertical Line'])
plt.xlabel('X')
plt.ylabel('X Squared')

plt.subplot(1, 2, 2)
plt.plot(x, -1 * (x ** 2))
plt.plot([-10, 0, 10], [-50, -50, -50])  <<<<<<======= ???????
plt.legend(['-X^2', 'Horizontal Line'])

plt.xlabel('X')
plt.ylabel('X Squared')

Upvotes: 0

Views: 40

Answers (1)

tmdavison
tmdavison

Reputation: 69116

From the docs for plt.plot():

Plot y versus x as lines and/or markers.

Call signature:

plot([x], y, [fmt], *, data=None, **kwargs)

The coordinates of the points or line nodes are given by x, y.

As you can see, the first argument to plt.plot() is a list of x-coordinates, and the second argument is a list of y-coordinates.

So, with plt.plot([0, 0, 0], [-10, 0, 100]) you are giving lists of x-coordinates ([0, 0, 0]) and y-coordinates ([-10, 0, 100]) to be plotted, not as you suggested two lists of (x, y, z) coordinates.

In the vertical line, all your x-coordinates are 0 with y-coordinates of -10, 0 and 100; in this example, the middle point (with coordinates (0, 0)) is unnecessary, you could just draw a line between (0, -10) and (0, 100) using plt.plot([0, 0], [-10, 100]).

Similarly for the horizontal line, you could reduce plt.plot([-10, 0, 10], [-50, -50, -50]) to plt.plot([-10, 10], [-50, -50]) to draw a line at y=-50 between x=-10 and x=10.

Upvotes: 1

Related Questions