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
Make a multiline plot from .CSV file in matplotlib
Creating multiline plots from CSV data is a common task in data visualization. Matplotlib combined with pandas makes this straightforward by reading CSV data and plotting multiple columns as separate lines on the same graph.
Steps to Create Multiline Plot
To make a multiline plot from a CSV file in matplotlib, follow these steps −
- Set the figure size and adjust the padding between and around the subplots
- Create a list of columns to fetch the data from a CSV file (ensure column names match those in the CSV)
- Read the data from the CSV file using pandas
- Plot the lines using df.plot() method
- Display the figure using show() method
Example with Sample Data
Let's create a multiline plot using sample automotive data ?
import pandas as pd
import matplotlib.pyplot as plt
# Set the figure size
plt.rcParams["figure.figsize"] = [10.00, 6.00]
plt.rcParams["figure.autolayout"] = True
# Create sample data (simulating CSV content)
data = {
'mpg': [18, 15, 18, 16, 17, 15, 14, 14, 14, 15],
'displacement': [307, 350, 318, 304, 302, 429, 454, 440, 455, 390],
'horsepower': [130, 165, 150, 150, 140, 198, 220, 215, 225, 190],
'weight': [3504, 3693, 3436, 3433, 3449, 4341, 4354, 4312, 4425, 3850]
}
df = pd.DataFrame(data)
# Normalize data for better visualization (scale to 0-100)
df_normalized = df.div(df.max()) * 100
# Plot the lines
ax = df_normalized.plot(kind='line', marker='o', linewidth=2, markersize=4)
ax.set_xlabel('Car Index')
ax.set_ylabel('Normalized Values (0-100)')
ax.set_title('Multiline Plot of Automotive Data')
ax.legend(loc='best')
ax.grid(True, alpha=0.3)
plt.show()
[A multiline plot will be displayed showing four lines representing mpg, displacement, horsepower, and weight data normalized to 0-100 scale]
Reading from Actual CSV File
When working with an actual CSV file, use this approach ?
import pandas as pd
import matplotlib.pyplot as plt
# Set the figure size
plt.rcParams["figure.figsize"] = [10.00, 6.00]
plt.rcParams["figure.autolayout"] = True
# Make a list of columns to plot
columns = ['mpg', 'displacement', 'horsepower', 'weight']
# Read CSV file with selected columns
df = pd.read_csv("auto-mpg.csv", usecols=columns)
# Plot the lines
ax = df.plot(kind='line', figsize=(10, 6))
ax.set_title('Multiline Plot from CSV Data')
ax.set_xlabel('Data Points')
ax.set_ylabel('Values')
ax.legend(loc='best')
ax.grid(True, alpha=0.3)
plt.show()
Customization Options
You can customize the multiline plot with various parameters ?
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'Sales_Q1': [100, 120, 140, 110, 130],
'Sales_Q2': [110, 130, 150, 120, 140],
'Sales_Q3': [120, 140, 160, 130, 150],
'Sales_Q4': [130, 150, 170, 140, 160]
}
df = pd.DataFrame(data)
# Create customized multiline plot
ax = df.plot(
kind='line',
figsize=(10, 6),
linewidth=2,
marker='o',
markersize=6,
color=['red', 'blue', 'green', 'orange']
)
ax.set_title('Quarterly Sales Data', fontsize=16, fontweight='bold')
ax.set_xlabel('Month', fontsize=12)
ax.set_ylabel('Sales (in thousands)', fontsize=12)
ax.legend(title='Quarters', loc='upper left')
ax.grid(True, linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()
[A customized multiline plot with colored lines, markers, and grid will be displayed]
Key Parameters
| Parameter | Purpose | Example |
|---|---|---|
kind='line' |
Specify plot type | Creates line plot |
figsize |
Set plot dimensions | (10, 6) for width×height |
marker |
Add data point markers | 'o' for circles, 's' for squares |
linewidth |
Control line thickness | 2 for thicker lines |
Conclusion
Creating multiline plots from CSV data combines pandas for data reading and matplotlib for visualization. Use df.plot() for quick plots or customize with additional parameters for professional-looking charts. Always normalize data when columns have different scales for better visualization.
