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
Selected Reading
How to plot CSV data using Matplotlib and Pandas in Python?
To plot CSV data using Matplotlib and Pandas in Python, we can read CSV files directly into a DataFrame and create visualizations. This approach combines the data manipulation power of Pandas with Matplotlib's plotting capabilities.
Creating Sample CSV Data
First, let's create a sample CSV file to demonstrate the plotting process ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'Age': [23, 25, 22, 24, 26],
'Marks': [85, 92, 78, 88, 95]
}
df = pd.DataFrame(data)
print("Sample DataFrame:")
print(df)
Sample DataFrame:
Name Age Marks
0 Alice 23 85
1 Bob 25 92
2 Charlie 22 78
3 Diana 24 88
4 Eve 26 95
Basic Line Plot
Plot Age and Marks columns with Name as the index ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'Age': [23, 25, 22, 24, 26],
'Marks': [85, 92, 78, 88, 95]
}
df = pd.DataFrame(data)
# Set figure size
plt.figure(figsize=(10, 6))
# Set Name as index and plot
df.set_index('Name').plot(kind='line', marker='o')
plt.title('Student Age and Marks')
plt.ylabel('Values')
plt.legend(['Age', 'Marks'])
plt.grid(True)
plt.show()
Bar Chart Visualization
Create a bar chart to compare student marks ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'Age': [23, 25, 22, 24, 26],
'Marks': [85, 92, 78, 88, 95]
}
df = pd.DataFrame(data)
# Create bar chart
plt.figure(figsize=(8, 6))
plt.bar(df['Name'], df['Marks'], color='skyblue', edgecolor='navy')
plt.title('Student Marks Comparison')
plt.xlabel('Student Name')
plt.ylabel('Marks')
plt.xticks(rotation=45)
plt.grid(axis='y', alpha=0.3)
plt.show()
Multiple Plot Types
Create subplots to display different visualizations ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'Age': [23, 25, 22, 24, 26],
'Marks': [85, 92, 78, 88, 95]
}
df = pd.DataFrame(data)
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Line plot
df.set_index('Name')['Marks'].plot(kind='line', ax=ax1, marker='o', color='red')
ax1.set_title('Marks Trend')
ax1.set_ylabel('Marks')
ax1.grid(True)
# Scatter plot
ax2.scatter(df['Age'], df['Marks'], color='green', s=100)
ax2.set_title('Age vs Marks')
ax2.set_xlabel('Age')
ax2.set_ylabel('Marks')
ax2.grid(True)
plt.tight_layout()
plt.show()
Key Features
-
Direct DataFrame plotting − Use
df.plot()for quick visualizations -
Index setting − Use
set_index()to define x-axis labels - Plot customization − Add titles, labels, and styling options
- Multiple chart types − Support for line, bar, scatter, and other plot types
Conclusion
Pandas and Matplotlib provide a powerful combination for visualizing CSV data. Use df.plot() for quick plots or combine with Matplotlib functions for advanced customization and multiple subplot layouts.
Advertisements
