Open In App

Matplotlib.pyplot.plot() function in Python

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The matplotlib.pyplot.plot() is used to create 2D plots such as line graphs and scatter plots. The plot() function allows us to plot data points, customize line styles, markers and colors making it useful for various types of visualizations. In this article, we’ll see how to use this function to plot data in Python.

Syntax: matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

Parameters:

  • x, y: Represent horizontal and vertical coordinates for the data points.
  • fmt: A format string that defines the line style, marker and colour.
  • data: The optional parameter can be an object containing labelled data which makes it easier to handle datasets directly.

Returns: A list of Line2D objects each representing a segment of the plotted data.

There are many ways of creating plot using Matplotlib.pyplot.plot() function some of their examples are:

Example 1: Line Plots in Matplotlib

Here we use Matplotlib‘s plot() function to create a simple line plot with the data [1, 2, 3, 4, 5].

Python
importmatplotlib.pyplotaspltplt.plot([1,2,3,4,5])plt.title('Basic Line Plot')plt.show()

Output:

matplot1

Basic Line Plot

Example 2: Multiple Lines Using Matplotlib

We will plot sine and cosine functions on the same graph.

  • plt.plot(x, y1, label=’Sin(x)’, color=’blue’): Plots sine curve with x values and y1 values labeling it as “Sin(x)” and using a blue color for the line.
  • plt.plot(x, y2, label=’Cos(x)’, color=’red’, linestyle=’–‘): Plots cosine curve with x values and y2 values labeling it as “Cos(x)” using a red color and a dashed line style.
Python
importmatplotlib.pyplotaspltimportnumpyasnpx=np.linspace(0,2*np.pi,100)y1=np.sin(x)y2=np.cos(x)plt.plot(x,y1,label='Sin(x)',color='blue')plt.plot(x,y2,label='Cos(x)',color='red',linestyle='--')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Multiple Lines Plot')plt.legend()plt.show()

Output:

matplot2

Multiple Lines Plot

Example 3: Scatter Plot with Custom Markers

We will generate and customize scatter plot with 50 random data points featuring red circular markers.

  • plt.plot(x, y, marker=’o’, linestyle=”, color=’red’, label=’Scatter Plot’): Plots a scatter plot with x and y values and using red circular markers (marker=’o’) with no connecting lines (linestyle=”) and labeling it as “Scatter Plot”.
Python
importmatplotlib.pyplotaspltimportnumpyasnpnp.random.seed(42)x=np.random.rand(50)y=np.random.rand(50)plt.plot(x,y,marker='o',linestyle='',color='red',label='Scatter Plot')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Scatter Plot Example')plt.legend()plt.show()

Output:

matplot3

Scatter Plot with Multiple Markers

Example 4: Plotting Multiple Curves

We are creating a line plot with two curves: a blue curve [Tex]y = x^2[/Tex]and an orange curve [Tex]y = 1 – x^3[/Tex] generating data randomly. The plot is limited to the range [0, 1] on both axes showcasing a visual representation of mathematical functions.

Python
importmatplotlib.pyplotaspltimportnumpyasnpnp.random.seed(19680801)xdata=np.random.random([2,10])xdata1=xdata[0,:]xdata2=xdata[1,:]xdata1.sort()xdata2.sort()ydata1=xdata1**2ydata2=1-xdata2**3plt.plot(xdata1,ydata1,color='tab:blue')plt.plot(xdata2,ydata2,color='tab:orange')plt.xlim([0,1])plt.ylim([0,1])plt.title('matplotlib.pyplot.plot() example 2')plt.show()

Output:

matplot4

Two Curves Plot

With the flexibility to customize line styles, markers and colors Matplotlib’s plot() function provides various possibilities for visualizing our data in Python.



Next Article

Similar Reads

close