Matplotlib.pyplot.plot() function in Python
Last Updated :
26 Apr, 2025
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
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4, 5])
plt.title('Basic Line Plot')
plt.show()
Output:
Basic Line PlotExample 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
import matplotlib.pyplot as plt
import numpy as np
x = 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:
Multiple Lines PlotExample 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
import matplotlib.pyplot as plt
import numpy as np
np.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:
Scatter Plot with Multiple MarkersExample 4: Plotting Multiple Curves
We are creating a line plot with two curves: a blue curve y = x^2and an orange curve y = 1 - x^3 generating data randomly. The plot is limited to the range [0, 1] on both axes showcasing a visual representation of mathematical functions.
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
xdata = np.random.random([2, 10])
xdata1 = xdata[0, :]
xdata2 = xdata[1, :]
xdata1.sort()
xdata2.sort()
ydata1 = xdata1 ** 2
ydata2 = 1 - xdata2 ** 3
plt.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:
Two Curves PlotWith the flexibility to customize line styles, markers and colors Matplotlib's plot() function provides various possibilities for visualizing our data in Python.
Similar Reads
Matplotlib.pyplot.pcolor() function in Python Matplotlib is the well-known Python package used in data visualization. Numpy is the numerical mathematics extension of Matplotlib. Matplotlib is capable of producing high-quality graphs, charts, and figures. Matplotlib produces object-oriented API for embedding plots into projects using GUI toolkit
2 min read
Matplotlib.pyplot.plot_date() function in Python Matplotlib is a module package or library in Python which is used for data visualization. Pyplot is an interface to a Matplotlib module that provides a MATLAB-like interface. The matplotlib.pyplot.plot_date() function is like the regular plot() function, but it's tailored for showing data over dates
3 min read
Matplotlib.pyplot.setp() function in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.fill() function in Python Matplotlib.pyplot.fill() function is used to fill the area enclosed by polygon /curve. Syntax: matplotlib.pyplot.fill(*args, data=None, **kwargs) Parameters: *args: sequence of x, y, [color] Â Â Â sequence of x,y = To traverse the boundaries of the polygon or curve defined by lists of x and y positi
2 min read
Matplotlib.pyplot.xscale() function in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.hexbin() function in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.figimage() function in Python Matplotlib is a widely used library in Python for plotting various graphs, as it provides very efficient ways and easy to understand methods for complex plots also. pyplot is a collection of command style functions that make matplotlib work like MATLAB. figimage() function matplotlib.pyplot.figimage
2 min read
Matplotlib.pyplot.get_plot_commands() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.suptitle() function in Python Matplotlib is a library in Python and it is a mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.suptitle() Function The suptitle() function in pyplot module of the matplotlib library is used to
3 min read
matplotlib.pyplot.semilogy() function in Python Matplotlib is the most popular and Python-ready package that is used for visualizing the data. We use matplotlib for plotting high-quality charts, graphs, and figures. matplotlib.pyplot.semilogy() Function The matplotlib.pyplot.semilogy() function in pyplot module of matplotlib library is used to ma
2 min read