Skip to content

Commit 3840d71

Browse files
author
studentpiyush
committed
Add pandas+matplotlib example (examples/library_examples/pandas_matplotlib)
1 parent a71618f commit 3840d71

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Pandas + Matplotlib Example
2+
3+
This example demonstrates a simple workflow using **pandas** and **matplotlib**:
4+
5+
- Create a small dataset using pandas
6+
- Perform a simple data transformation (moving average)
7+
- Visualize the results with matplotlib (line chart)
8+
9+
## Files
10+
11+
- `example.py` — main script demonstrating the workflow
12+
13+
## Requirements
14+
15+
```bash
16+
pip install pandas matplotlib
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Example: Using pandas and matplotlib together
3+
4+
This script demonstrates how to:
5+
1. Create a DataFrame using pandas
6+
2. Perform a simple transformation
7+
3. Visualize the results using matplotlib
8+
9+
Requirements:
10+
pip install pandas matplotlib
11+
"""
12+
13+
import pandas as pd
14+
import matplotlib.pyplot as plt
15+
16+
# Step 1: Create a sample DataFrame
17+
data = {
18+
"Month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
19+
"Sales": [250, 300, 280, 350, 400, 380]
20+
}
21+
df = pd.DataFrame(data)
22+
23+
# Step 2: Add a moving average column
24+
df["Moving_Avg"] = df["Sales"].rolling(window=2).mean()
25+
26+
# Step 3: Plot the data
27+
plt.figure(figsize=(8, 5))
28+
plt.plot(df["Month"], df["Sales"], marker='o', label="Sales", color="blue")
29+
plt.plot(df["Month"], df["Moving_Avg"], marker='s', label="Moving Avg", linestyle="--", color="orange")
30+
31+
plt.title("Monthly Sales with Moving Average")
32+
plt.xlabel("Month")
33+
plt.ylabel("Sales")
34+
plt.legend()
35+
plt.grid(True, linestyle="--", alpha=0.6)
36+
plt.tight_layout()
37+
plt.show()
38+
39+
# Optional: Print the DataFrame for reference
40+
print(df)

0 commit comments

Comments
 (0)