
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Line Plot
A line plot is a visual representation of data where individual points are connected by straight lines. It is mainly used to observe relationships between two variables on x-axis and y-axis.
This plot helps you to visualize fluctuations, patterns, trends, or progressions in your data. For instance, let us create a graph where you have the student attendance over a specific period, such as months or semesters. The x-axis will represent the months, and the y-axis will represent the attendance in percent sign −

In this tutorial, we will learn how to create and customize line plots using the Pandas library in Python.
Line Plot in Pandas
Pandas provides the plot.line() method to create line plots from Series and DataFrames. This method internally uses Matplotlib and returns a matplotlib.axes.Axes object or an NumPy array np.ndarray of Axes when subplots parameter is set to True.
DataFrame.plot.line(): Creates line plot for one or more columns in a DataFrame.
Series.plot.line(): Creates a line plot for a single Series.
Syntax
The following is the syntax of the plot.line() method for both the Series and DataFrames objects −
DataFrame.plot.line(x=None, y=None, **kwargs)
Parameters,
x: The column label or index position to be plotted on the x-axis. If not specified, the DataFrame index is used.
y: The column label or index position to be plotted on the y-axis. If not specified, all numerical columns are used.
**kwargs: Additional keyword arguments to customize the plot appearance.
Example: Creating Line Plot for Series Data
This example demonstrates using the Series.plot.line() method on a Pandas Series object.
import pandas as pd import numpy as np import matplotlib.pyplot as plt # Create a Pandas Series series = pd.Series(3 * np.random.rand(4), index=["a", "b", "c", "d"]) # Generate a line plot series.plot.line(figsize=(7, 4)) # Set title and Display the plot plt.title('Basic line Plot') plt.show()
Following is the output of the above code −

Example: Creating Line Plot for a DataFrame
This example demonstrates how to create a line plot for multiple columns in a DataFrame using the DataFrame.plot.line() method.
import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with population data df = pd.DataFrame({ 'Pig': [20, 18, 489, 675, 1776], 'Horse': [4, 25, 281, 600, 1900] }, index=[1990, 1997, 2003, 2009, 2014]) # Generate a line plot df.plot.line() # Show the plot plt.title('Animal Population Over Time') plt.xlabel('Year') plt.ylabel('Population') plt.show()
After executing the above code, we get the following output −

Customizing a Line Plot
Pandas allows customization of line plots through various parameters such as, labels, colors, autopct, fontsize, and more.
Example
This example demonstrates customizing the line plot using the addition keyword arguments. Here we will create the separate subplots by setting the subplots=True and specified the different colors for each column using the color parameter.
import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with population data df = pd.DataFrame({ 'Pig': [20, 18, 489, 675, 1776], 'Horse': [4, 25, 281, 600, 1900] }, index=[1990, 1997, 2003, 2009, 2014]) # Customizing line colors df.plot.line(subplots=True, color={"Pig": "pink", "Horse": "brown"}) # Show the plot plt.title('Animal Population Over Time') plt.xlabel('Year') plt.ylabel('Population') plt.show()
Following is the output of the above code −

Line Plot One Column Against Another
Pandas plot.line() method easily draw line plot one column against another column by specifying.
Example
This example demonstrates how to plot one column against another using the plot.line() method.
import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with population data df = pd.DataFrame({ 'Pig': [20, 18, 489, 675, 1776], 'Horse': [4, 25, 281, 600, 1900] }, index=[1990, 1997, 2003, 2009, 2014]) # Plotting Horse population against Pig population df.plot.line(x='Pig', y='Horse') # Show the plot plt.title('Horse Population vs Pig Population') plt.xlabel('Pig Population') plt.ylabel('Horse Population') plt.show()
On executing the above code we will get the following output −
