Open In App
Related Articles

Matplotlib.pyplot.plot() function in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Matplotlib is a powerful Python library that serves as a numerical and mathematical extension for the NumPy library. One of its key components is Pyplot, which offers a state-based interface to the Matplotlib module, presenting users with a familiar MATLAB-like environment. Through Matplotlib.pyplot.plot() function in Python, users can effortlessly create a variety of plots, including Line Plots, Contour Plots, Histograms, Scatter Plots, 3D Plots, and more. This versatility makes Matplotlib an invaluable tool for data visualization and analysis in the Python programming language.

What is Matplotlib.pyplot.plot() Function?

The matplotlib.pyplot.plot() function is a fundamental component of the Matplotlib library, specifically within the Pyplot module. It serves the purpose of generating a 2D hexagonal binning plot based on the given data points represented by the variables x and y. It connects data points with lines, allowing customization of the plot’s appearance through parameters such as line styles and markers. This versatile function is widely used for data visualization in various domains.

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

  • Parameters:
    • x, y: These parameters represent the horizontal and vertical coordinates of the data points. The ‘x’ values are optional, allowing flexibility in the plotting process.
    • fmt: This is an optional parameter that contains a string value. It is used to specify the format of the plot, defining the line style, marker, and color.
    • data: An optional parameter, ‘data’ refers to an object with labeled data. It provides a convenient way to pass data directly, enhancing the readability and ease of use.
  • Returns: The plot() function returns a list of Line2D objects, each representing a segment of the plotted data. These Line2D objects encapsulate the characteristics and attributes of the plotted lines, allowing for further customization and analysis.

Matplotlib.pyplot.plot() Function in Python

There are various way of creating plot using Matplotlib.pyplot.plot() function in Python, Their are some examples illustrate the matplotlib.pyplot.plot() function in matplotlib.pyplot:

  • Basic Line Plot
  • Multiple Lines Plot
  • Scatter Plot with Multiple Markers
  • Two Curves Plot

Line Plots in Matplotlib

By importing Matplotlib’s plot() we created a line plot with data [1, 2, 3]. The title() function sets the plot title, draw() updates the plot, and show() displays it, providing a basic illustration of Matplotlib for data visualization in Python.

Python3

import matplotlib.pyplot as plt
import numpy as np
 
# Plotting a simple line graph
plt.plot([1, 2, 3])
 
# Setting the title
plt.title('Matplotlib Line Plot Example')
 
# Updating and displaying the plot
plt.draw()
plt.show()

                    

Output:

first

Basic Line Plot

Multiple Lines Using Matplotlib

By importing Matplotlib to plot sine and cosine functions on the same graph. It generates data, sets styles for each function, adds labels and a title, displays a legend, and then shows the plot, illustrating the sine and cosine curves.

Python3

import matplotlib.pyplot as plt
import numpy as np
 
# Generate data
x = np.linspace(0, 2*np.pi, 100)
y1, y2 = np.sin(x), np.cos(x)
 
# Plotting multiple lines on a single plot
plt.plot(x, y1, label='Sin(x)', color='b')
plt.plot(x, y2, label='Cos(x)', color='r', linestyle='--')
 
# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Multiple Lines Plot')
 
# Displaying the legend and the plot
plt.legend()
plt.show()

                    

Output

second

Multiple Lines Plot

Markers in Matplotlib

By importing Matplotlib we generated a customized scatter plot with 50 random data points, featuring red circular markers. It includes axis labels, a title (‘Scatter Plot Example’), and a legend. The show() function displays the plot, demonstrating a basic example of data visualization with Matplotlib in Python.

Python3

import matplotlib.pyplot as plt
import numpy as np
 
# Generate random data
np.random.seed(42)
x = np.random.rand(50)
y = np.random.rand(50)
 
# Plotting a scatter plot with custom markers
plt.plot(x, y, marker='o', linestyle='', markersize=8, color='r', label='Scatter Plot')
 
# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot Example')
 
# Displaying the legend
plt.legend()
 
# Display the plot
plt.show()

                    

Output:

3

Scatter Plot with Multiple Markers

Plotting Multiple Curves

By importing Matplotlib we created a line plot with two curves: a blue curve (y=x^2) and an orange curve (y=1−x^3). The data is generated randomly, sorted for smooth curves, and plotted with the plot() function. The plot is limited to the range [0, 1] on both axes, showcasing a visual representation of mathematical functions.

Python3

# Implementation of matplotlib function
   
import matplotlib.pyplot as plt
import numpy as np
   
# Fixing random state for reproducibility
np.random.seed(19680801)
   
# create random data
xdata = np.random.random([2, 10])
   
# split the data into two parts
xdata1 = xdata[0, :]
xdata2 = xdata[1, :]
   
# sort the data so it makes clean curves
xdata1.sort()
xdata2.sort()
   
# create some y data points
ydata1 = xdata1 ** 2
ydata2 = 1 - xdata2 ** 3
   
# plot the data
plt.plot(xdata1, ydata1, color ='tab:blue')
plt.plot(xdata2, ydata2, color ='tab:orange')
   
   
# set the limits
plt.xlim([0, 1])
plt.ylim([0, 1])
 
plt.title('matplotlib.pyplot.plot() example 2')
   
# display the plot
plt.show()

                    

Output

last

Two Curves Plot

Conclusion

In conclusion, the matplotlib.pyplot.plot() function in Python is a fundamental tool for creating a variety of 2D plots, including line plots, scatter plots, and more. Its versatility allows users to customize plots by specifying data points, line styles, markers, and colors. With optional parameters such as ‘fmt’ and ‘data,’ the function provides flexibility in plot formatting and data handling. Additionally, the returned Line2D objects allow for further manipulation and analysis of the plotted data. Overall, Matplotlib’s plot() function is a key component in the realm of data visualization, offering a user-friendly interface for creating insightful and visually appealing plots in Python.



Last Updated : 29 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads