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
Plotting multiple line graphs using Pandas and Matplotlib
To plot multiple line graphs using Pandas and Matplotlib, we can create a DataFrame with different datasets and use the plot() method to visualize multiple lines on the same graph. This approach is useful for comparing trends across different data series.
Basic Setup
First, let's set up the required libraries and configure the plot settings ?
import pandas as pd import matplotlib.pyplot as plt # Set figure size for better visualization plt.rcParams["figure.figsize"] = [10, 6] plt.rcParams["figure.autolayout"] = True
Creating Sample Data
We'll create a DataFrame with data for three different mathematical functions ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data for multiple equations
data = [
["y=x^3", 0, 0],
["y=x^3", 1, 1],
["y=x^3", 2, 8],
["y=x^3", 3, 27],
["y=x^3", 4, 64],
["y=x^2", 0, 0],
["y=x^2", 1, 1],
["y=x^2", 2, 4],
["y=x^2", 3, 9],
["y=x^2", 4, 16],
["y=2x", 0, 0],
["y=2x", 1, 2],
["y=2x", 2, 4],
["y=2x", 3, 6],
["y=2x", 4, 8],
]
df = pd.DataFrame(data, columns=['equation', 'x', 'y'])
print("Original DataFrame:")
print(df.head(10))
Original DataFrame: equation x y 0 y=x^3 0 0 1 y=x^3 1 1 2 y=x^3 2 8 3 y=x^3 3 27 4 y=x^3 4 64 5 y=x^2 0 0 6 y=x^2 1 1 7 y=x^2 2 4 8 y=x^2 3 9 9 y=x^2 4 16
Reshaping Data with pivot()
To plot multiple lines, we need to reshape the DataFrame using pivot() ?
import pandas as pd
import matplotlib.pyplot as plt
# Create and reshape the data
data = [
["y=x^3", 0, 0], ["y=x^3", 1, 1], ["y=x^3", 2, 8], ["y=x^3", 3, 27], ["y=x^3", 4, 64],
["y=x^2", 0, 0], ["y=x^2", 1, 1], ["y=x^2", 2, 4], ["y=x^2", 3, 9], ["y=x^2", 4, 16],
["y=2x", 0, 0], ["y=2x", 1, 2], ["y=2x", 2, 4], ["y=2x", 3, 6], ["y=2x", 4, 8],
]
df = pd.DataFrame(data, columns=['equation', 'x', 'y'])
# Pivot the DataFrame
df_pivoted = df.pivot(index='x', columns='equation', values='y')
print("Pivoted DataFrame:")
print(df_pivoted)
Pivoted DataFrame: equation y=2x y=x^2 y=x^3 x 0 0 0 0 1 2 1 1 2 4 4 8 3 6 9 27 4 8 16 64
Plotting Multiple Lines
Now we can create the multiple line plot using the plot() method ?
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [10, 6]
# Create and reshape the data
data = [
["y=x^3", 0, 0], ["y=x^3", 1, 1], ["y=x^3", 2, 8], ["y=x^3", 3, 27], ["y=x^3", 4, 64],
["y=x^2", 0, 0], ["y=x^2", 1, 1], ["y=x^2", 2, 4], ["y=x^2", 3, 9], ["y=x^2", 4, 16],
["y=2x", 0, 0], ["y=2x", 1, 2], ["y=2x", 2, 4], ["y=2x", 3, 6], ["y=2x", 4, 8],
]
df = pd.DataFrame(data, columns=['equation', 'x', 'y'])
df_pivoted = df.pivot(index='x', columns='equation', values='y')
# Create the plot
df_pivoted.plot(marker='o', linewidth=2)
plt.title('Multiple Line Graphs: Mathematical Functions')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.grid(True, alpha=0.3)
plt.legend(title='Equations')
plt.show()
Alternative Method: Direct DataFrame Plotting
You can also create multiple line plots directly from a DataFrame without pivoting ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample time series data
dates = pd.date_range('2023-01-01', periods=10, freq='D')
data = {
'Product A': [10, 12, 8, 15, 18, 20, 16, 22, 25, 28],
'Product B': [8, 9, 12, 11, 14, 16, 18, 19, 21, 24],
'Product C': [15, 13, 16, 14, 17, 19, 18, 20, 23, 26]
}
df_sales = pd.DataFrame(data, index=dates)
# Plot multiple lines
plt.figure(figsize=(10, 6))
df_sales.plot(kind='line', marker='s', linewidth=2)
plt.title('Sales Comparison Over Time')
plt.xlabel('Date')
plt.ylabel('Sales Units')
plt.grid(True, alpha=0.3)
plt.legend(title='Products')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Key Parameters for Customization
| Parameter | Description | Example |
|---|---|---|
marker |
Add markers to data points | 'o', 's', '^' |
linewidth |
Set line thickness | 1, 2, 3 |
alpha |
Set line transparency | 0.5, 0.7, 1.0 |
color |
Specify line colors | ['red', 'blue', 'green'] |
Conclusion
Plotting multiple line graphs with Pandas and Matplotlib is straightforward using the pivot() method to reshape data and the plot() method for visualization. This approach is excellent for comparing trends across different data series in a single, clear visualization.
