Python Pandas DataFrame plot.line() Function
A line plot, often known as a line chart, is a type of chart that displays data as a series of data points connected by straight line segments. It’s similar to a scatter plot, but the measurement points are ordered (often by x-axis value) and connected with straight line segments. A line plot is frequently used to visualize a data trend.
A line plot is produced using the DataFrame.plot.line() function.
Syntax:
DataFrame.plot.line(x=None, y=None)
Parameters
x: This is optional. It indicates the label or position. It allows for the plotting of one column versus another. If no index is provided, the DataFrame’s index is taken. The type of this may be an int or a string.
y: This is optional. It indicates the label or position. It allows for the plotting of one column versus another. If no index is provided, all numerical columns are utilized. The type of this may be an int or a string.
Color (defines the colour of the line), title (defines the title of the plot), and kind(specifies which type of plot to use) are additional parameters. This method’s default variable for the “kind” parameter is ‘line’. As a result, you do not need to set it in order to produce a line plot.
Return Value:
matplotlib.axes.Axes or a ndarray is returned containing one matplotlib.axes.Axes per column when subplots=True.
- Python Pandas DataFrame mul() Function
- Python Pandas DataFrame div() Function
- Python Pandas DataFrame sub() Function
Pandas DataFrame plot.line() Function in Python:
Example1
Approach:
- Import pandas module using the import keyword.
- Import pyplot function of the matplotlib module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Plot the line graph of the firstyear_marks column of the given dataframe using the dataframe.plot.line() function
- Display the plot using the show() function of the matplotlib module.
-
The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Import pyplot function of the matplotlib module using the import keyword. import matplotlib.pyplot as plt # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "firstyear_marks": [71, 60, 35, 85], "secondyear_marks": [80, 40, 25, 90], "thirdyear_marks": [90, 73, 68, 80], "fourthyear_marks": [75, 80, 55, 95]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Plot the line graph of the firstyear_marks column of the # given dataframe using the dataframe.plot.line() function data_frme.plot.line(y=['firstyear_marks']) # Display the plot using the show() function of the matplotlib module plt.show()
Output:
The given Dataframe: firstyear_marks secondyear_marks thirdyear_marks fourthyear_marks virat 71 80 90 75 nick 60 40 73 80 jessy 35 25 68 55 sindhu 85 90 80 95
Example2
Here, the line plot is plotted for the selected columns i.e, firstyear_marks, secondyear_marks of the given dataframe.
Approach:
- Import pandas module using the import keyword.
- Import pyplot function of the matplotlib module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Plot the line graph of firstyear_marks, secondyear_marks columns of the given dataframe using the dataframe.plot.line() function by passing the argument as a list.
- Display the plot using the show() function of the matplotlib module.
-
The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Import pyplot function of the matplotlib module using the import keyword. import matplotlib.pyplot as plt # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "firstyear_marks": [71, 60, 35, 85], "secondyear_marks": [80, 40, 25, 90], "thirdyear_marks": [90, 73, 68, 80], "fourthyear_marks": [75, 80, 55, 95]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Plot the line graph of firstyear_marks, secondyear_marks columns # of the given dataframe using the dataframe.plot.line() function by passing the # argument as a list. data_frme.plot.line(y=['firstyear_marks', 'secondyear_marks']) # Display the plot using the show() function of the matplotlib module plt.show()
Output:
The given Dataframe: firstyear_marks secondyear_marks thirdyear_marks fourthyear_marks virat 71 80 90 75 nick 60 40 73 80 jessy 35 25 68 55 sindhu 85 90 80 95
Example3
Here, the line graph is split column-wise by using the subplots=True argument.
Approach:
- Import pandas module using the import keyword.
- Import pyplot function of the matplotlib module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
-
Plot the line graph of firstyear_marks, secondyear_marks columns of the given dataframe using the dataframe.plot.line() function by passing the argument as a list, and subplots=True.Here, the line graph is split column-wise by using the subplots=True argument.
- Display the plot using the show() function of the matplotlib module.
-
The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Import pyplot function of the matplotlib module using the import keyword. import matplotlib.pyplot as plt # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "firstyear_marks": [71, 60, 35, 85], "secondyear_marks": [80, 40, 25, 90], "thirdyear_marks": [90, 73, 68, 80], "fourthyear_marks": [75, 80, 55, 95]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Plot the line graph of firstyear_marks, secondyear_marks columns # of the given dataframe using the dataframe.plot.line() function by passing the # argument as a list, and subplots=True. # Here, the line graph is split column-wise by using the subplots=True argument data_frme.plot.line(y=['firstyear_marks', 'secondyear_marks'], subplots=True) # Display the plot using the show() function of the matplotlib module plt.show()
Output:
The given Dataframe: firstyear_marks secondyear_marks thirdyear_marks fourthyear_marks virat 71 80 90 75 nick 60 40 73 80 jessy 35 25 68 55 sindhu 85 90 80 95