A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/matplotlib-pyplot-plot-function-in-python/ below:

Matplotlib.pyplot.plot() function in Python - GeeksforGeeks

Matplotlib.pyplot.plot() function in Python

Last Updated : 15 Jul, 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:

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 Plot Example 2: Multiple Lines Using Matplotlib

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

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 Plot Example 3: Scatter Plot with Custom Markers

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

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 Markers Example 4: Plotting Multiple Curves

We are creating a line plot with two curves: a blue curve y = x^2 and 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 Plot

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



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4