Lecture

Plotting with plt.plot()


The plt.plot() function is the most commonly used method in Matplotlib. It lets you create simple line plots by specifying the data points for the x-axis and y-axis.

If you only pass one list of values, Matplotlib assumes it’s the y-axis and automatically assigns numbers starting from 0 for the x-axis.


Plotting X and Y Values

When both x and y values are provided, Matplotlib connects the dots with a straight line by default.

Plotting X and Y Values
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 20, 25, 30, 40] plt.plot(x, y) plt.title("Sample Line Plot") plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.show()
  • The order of x and y matters — both lists must be the same length.
  • If the plot looks wrong, check that your data is properly aligned.

Plotting Only Y Values

You can also plot a list of values without specifying x. In that case, the x-axis will automatically be set to [0, 1, 2, ...].

Plotting Only Y Values
y = [5, 9, 4, 7] plt.plot(y) plt.title("Auto X-Axis") plt.ylabel("Y Values") plt.show()

This is useful for quick visual checks of any 1D data.


What’s Next?

Now that you’ve learned how to plot lines using plt.plot(), the next step is to learn how to customize the lines with different colors, markers, and styles.

Quiz
0 / 1

What happens when you use plt.plot() with only one list of values?

It plots the values as the x-axis and assumes y-values from 0 upwards.

It creates an error because both x and y values are required.

It plots the values as the y-axis and automatically assigns x-values starting from 0.

It creates a scatter plot instead of a line plot.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help