Matplotlib – Plot Y versus X

To plot Y versus X using matplotlib, call plot() function on matplotlib.pyplot object.

The definition of plot() function is

</>
Copy
plot(*args, scalex=True, scaley=True, data=None, **kwargs)

The following are some of the sample plot() function calls with different set of parameters.

</>
Copy
plot(x, y)
plot([x], y, [fmt], *, data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

Example

In this example, we will plot Y vs X where x and y are lists of numbers. The respective number from the list x and list y at index i translate to a point on the map.

example.py

</>
Copy
import matplotlib.pyplot as plt

# x axis and y axis data
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
y = [5, 7, 8, 1, 4, 9, 6, 3, 5, 2, 1, 8]

#plot y vs x
plt.plot(x, y)

#set title and x, y - axes labels
plt.title('Matplotlib Example')
plt.xlabel('x-axis label')
plt.ylabel('y-axis label')

#show plot to user
plt.show()

Output

Matplotlib - Plot Y versus X
Plot Y vs X

Conclusion

Concluding this Matplotlib Tutorial, we learned how to plot Y vs X using Axes.plot() function in Matplotlib Python library.