What is matplotlib.pyplot.plot() in Python?

Data visualization is a significant aspect of data science. It is used to present data in a sensible manner for statistical or comparative analysis.

matplotlib is a Python library that provides a multitude of functions to visualize and plot data.

The matplotlib.pyplot.plot() function is used to plot two scalar values against each other as lines or markers.

Syntax

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

Parameters

  • x,y: Can be arrays or scalars. They correspond to the x-axis and y-axis values. The x-axis values are optional and the default value of x is range(len(y)).

  • fmt: A string value that specifies the formatting of the lines. (Optional)

  • data: A object that contains labeled data. (Optional)

Return value

matplotlib.pyplot.plot() returns a list of Line2D objects that represent the data.

Code

The code below plots a line that joins multiple points.

#import libraries
import matplotlib.pyplot as plt
import numpy as np
#initialize x-axis and y-axis values
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
#plot a line
plt.plot(x, y)
plt.show()
widget

To plot points without a line, the relevant format string is passed as parameter.

#import libraries
import matplotlib.pyplot as plt
import numpy as np
#initialize y-axis values with default x-axis values
y = np.array([5, 10, 7, 3])
#plot points only
plt.plot(x, y, 'o')
plt.show()
widget

Free Resources