Pandas Built-in Plotting (.plot())
Last Updated: 08 Nov 2025
Pandas Support built-in plotting using .plot().I
Hinglish Tip: .plot() = "data dekho, graph dikhao" — no plt.show(), no import matplotlib!
Syntax
df.plot(
kind='line', # chart type
x='column', # optional
y='column', # optional
title="Title",
figsize=(10,6),
grid=True
)
1. Line Plot (Time Series)
import pandas as pd
import numpy as np
# Sample sales data
dates = pd.date_range('2025-01-01', periods=10)
df = pd.DataFrame({
'date': dates,
'sales': [100, 120, 115, 140, 160, 155, 180, 200, 190, 220],
'profit': [20, 25, 22, 30, 35, 32, 40, 45, 42, 50]
}, index=dates)
df['sales'].plot(
title="Daily Sales Trend",
figsize=(10,4),
grid=True,
color='green'
)
2. Bar Plot (Categorical)
# City-wise customers
df_city = pd.DataFrame({
'city': ['Delhi', 'Mumbai', 'Bangalore', 'Kolkata', 'Chennai'],
'customers': [500, 420, 380, 290, 310]
})
df_city.plot(
x='city',
y='customers',
kind='bar',
title="Customers by City",
color='skyblue',
figsize=(8,5)
)
Horizontal Bar:
df_city.plot(x='city', y='customers', kind='barh', color='orange')
3. Histogram (Distribution)
# Age distribution
ages = np.random.normal(35, 10, 1000)
df_age = pd.DataFrame({'age': ages})
df_age['age'].plot(
kind='hist',
bins=30,
title="Age Distribution",
color='purple',
alpha=0.7,
figsize=(8,5)
)
4. Scatter Plot (Correlation)
df.plot(
x='sales',
y='profit',
kind='scatter',
title="Sales vs Profit",
color='red',
alpha=0.6,
figsize=(8,6)
)
5. Box Plot (Outliers)
# Exam scores
scores = pd.DataFrame({
'Math': np.random.normal(75, 15, 100),
'Science': np.random.normal(78, 12, 100),
'English': np.random.normal(72, 18, 100)
})
scores.plot(
kind='box',
title="Exam Scores Distribution",
figsize=(8,5),
grid=True
)
6. Area Plot (Stacked Trends)
df[['sales', 'profit']].plot(
kind='area',
title="Sales & Profit Over Time",
alpha=0.6,
figsize=(10,5)
)
7. Pie Chart (Proportions)
dept_count = pd.Series({
'IT': 50,
'HR': 20,
'Sales': 30,
'Marketing': 25
})
dept_count.plot(
kind='pie',
title="Department Strength",
autopct='%1.1f%%',
figsize=(6,6),
startangle=90
)
Pro: Chaining with groupby()
# Top 5 cities by sales
(df.groupby('city')['sales']
.sum()
.sort_values(ascending=False)
.head(5)
.plot.bar(title="Top 5 Cities by Sales", color='gold'))
All kind Options
Save Plot
# Save as PNG
plot = df['sales'].plot(title="Sales")
plot.figure.savefig("sales_plot.png", dpi=300, bbox_inches='tight')
Common Mistakes to Avoid
|| राम नाम सत्य है ||
