0
0
Matplotlibdata~5 mins

Basic plt.plot usage in Matplotlib

Choose your learning style9 modes available
Introduction

We use plt.plot to draw simple line graphs. It helps us see how numbers change over time or compare values.

To show how sales change each month.
To compare temperatures over days.
To track stock prices over time.
To visualize simple trends in data.
To quickly check relationships between two sets of numbers.
Syntax
Matplotlib
plt.plot(x, y, format_string, **kwargs)
plt.show()

x and y are lists or arrays of numbers.

format_string is optional and controls line style and color.

Examples
Draws a simple line connecting points (1,4), (2,5), and (3,6).
Matplotlib
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
Draws red circles connected by lines.
Matplotlib
plt.plot([1, 2, 3], [4, 5, 6], 'ro-')
plt.show()
Draws a green dashed line.
Matplotlib
plt.plot([0, 1, 2], [0, 1, 4], linestyle='--', color='green')
plt.show()
Sample Program

This program plots numbers and their squares with blue circles connected by lines. It adds labels and a grid for clarity.

Matplotlib
import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]

plt.plot(x, y, 'b-o')  # blue line with circle markers
plt.title('Square Numbers')
plt.xlabel('Number')
plt.ylabel('Square')
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Always call plt.show() to display the plot.

You can customize colors and line styles using format strings or keyword arguments.

If x is not given, plt.plot uses the index of y values automatically.

Summary

plt.plot draws lines connecting points.

You provide x and y data to plot.

Use plt.show() to see the graph.