Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Plot a Line Graph for Pandas Dataframe with Matplotlib?
We will plot a line graph for Pandas DataFrame using the plot() method. Line graphs are excellent for visualizing trends and relationships between numerical data over time or other continuous variables.
Basic Setup
First, import the required libraries ?
import pandas as pd import matplotlib.pyplot as plt
Creating the DataFrame
Let's create a DataFrame with car data to demonstrate line plotting ?
import pandas as pd
import matplotlib.pyplot as plt
# Create a DataFrame with car sales data
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Reg_Price": [2000, 2500, 2800, 3000, 3200, 3500],
"Units": [100, 120, 150, 170, 180, 200]
})
print(dataFrame)
Car Reg_Price Units
0 BMW 2000 100
1 Lexus 2500 120
2 Audi 2800 150
3 Mustang 3000 170
4 Bentley 3200 180
5 Jaguar 3500 200
Plotting the Line Graph
Use plt.plot() to create a line graph showing the relationship between price and units sold ?
import pandas as pd
import matplotlib.pyplot as plt
# Create the DataFrame
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Reg_Price": [2000, 2500, 2800, 3000, 3200, 3500],
"Units": [100, 120, 150, 170, 180, 200]
})
# Plot line graph
plt.plot(dataFrame["Reg_Price"], dataFrame["Units"], marker='o')
plt.xlabel('Registration Price')
plt.ylabel('Units Sold')
plt.title('Car Sales: Price vs Units')
plt.grid(True)
plt.show()
Alternative Method Using DataFrame.plot()
You can also use the DataFrame's built-in plot() method for more convenience ?
import pandas as pd
import matplotlib.pyplot as plt
# Create the DataFrame
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Reg_Price": [2000, 2500, 2800, 3000, 3200, 3500],
"Units": [100, 120, 150, 170, 180, 200]
})
# Using DataFrame.plot() method
dataFrame.plot(x='Reg_Price', y='Units', kind='line', marker='o',
title='Car Sales Analysis', grid=True)
plt.xlabel('Registration Price')
plt.ylabel('Units Sold')
plt.show()
Multiple Lines on Same Graph
You can plot multiple columns as separate lines for comparison ?
import pandas as pd
import matplotlib.pyplot as plt
# Create DataFrame with additional data
dataFrame = pd.DataFrame({
"Month": [1, 2, 3, 4, 5, 6],
"BMW_Sales": [100, 120, 110, 130, 140, 160],
"Audi_Sales": [90, 100, 115, 125, 135, 150]
})
# Plot multiple lines
plt.plot(dataFrame["Month"], dataFrame["BMW_Sales"], marker='o', label='BMW')
plt.plot(dataFrame["Month"], dataFrame["Audi_Sales"], marker='s', label='Audi')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Monthly Car Sales Comparison')
plt.legend()
plt.grid(True)
plt.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
marker |
Add markers to data points | 'o', 's', '^' |
label |
Legend label for the line | 'Sales Data' |
grid |
Show grid lines | True/False |
kind |
Plot type (DataFrame.plot) | 'line', 'bar', 'scatter' |
Conclusion
Use plt.plot() for basic line graphs or DataFrame.plot() for integrated pandas plotting. Add markers, labels, and grid for better visualization and data interpretation.
