Saturday, November 16, 2024
Google search engine
HomeLanguagesMatplotlib.pyplot.plot() function in Python

Matplotlib.pyplot.plot() 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, etc.

matplotlib.pyplot.plot() Function

The plot() function in pyplot module of matplotlib library is used to make a 2D hexagonal binning plot of points x, y.

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

Parameters: This method accept the following parameters that are described below:

  • x, y: These parameter are the horizontal and vertical coordinates of the data points. x values are optional.
  • fmt: This parameter is an optional parameter and it contains the string value.
  • data: This parameter is an optional parameter and it is an object with labelled data.

Returns: This returns the following:

  • lines : This returns the list of Line2D objects representing the plotted data.
  • Below examples illustrate the matplotlib.pyplot.plot() function in matplotlib.pyplot:

    Example 1:




    # Implementation of matplotlib function 
          
    import matplotlib.pyplot as plt 
    import numpy as np 
        
    plt.plot([1, 2, 3]) 
    plt.title('matplotlib.pyplot.plot() example 1'
    plt.draw() 
    plt.show() 

    
    

    Output:

    Example 2:




    # 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:

    RELATED ARTICLES

    Most Popular

    Recent Comments