💡 The Power of `DataFrame.plot()`
Pandas includes a powerful built-in plotting interface based on the Matplotlib library. By calling df.plot() on any Series or DataFrame, you can instantly generate line charts, bar graphs, scatter plots, histograms, and box plots without writing complex boilerplates.
1. Stock Price Trend Line Chart (`kind='line'`)
Visualizing weekly closing prices for a technology stock to inspect market trends over time.
Python (plot_line_stock.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_stock_trend():
data = {'Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], 'Price': [150, 153, 151, 156, 160]}
df = pd.DataFrame(data).set_index('Day')
# Generate line plot
df.plot(kind='line', marker='o', color='#0ea5e9', title='Weekly Stock Price Trend')
plt.ylabel('Price in USD')
plt.grid(True, linestyle='--', alpha=0.6)
plt.savefig('plot_line.png')
plot_stock_trend()
Rendered Plot Output (Line Chart):
2. Regional Sales Comparison Bar Chart (`kind='bar'`)
Comparing quarterly sales performance across different geographical regions using vertical bars.
Python (plot_bar_sales.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_regional_sales():
data = {'Region': ['North', 'South', 'East', 'West'], 'Sales_k': [45, 60, 35, 80]}
df = pd.DataFrame(data).set_index('Region')
df.plot(kind='bar', color='#6c2bd9', title='Regional Sales Performance')
plt.ylabel('Sales in $1000s')
plt.savefig('plot_bar.png')
plot_regional_sales()
Rendered Plot Output (Bar Chart):
3. Advertising Spend vs Revenue Scatter Plot (`kind='scatter'`)
Analyzing correlation between marketing ad spend and resulting revenue.
Python (plot_scatter.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_correlation():
data = {'Ad_Spend': [10, 20, 30, 40, 50], 'Revenue': [15, 28, 42, 55, 70]}
df = pd.DataFrame(data)
df.plot(kind='scatter', x='Ad_Spend', y='Revenue', color='#087f69', s=70, title='Ad Spend vs Revenue')
plt.grid(True, linestyle=':')
plt.savefig('plot_scatter.png')
plot_correlation()
Rendered Plot Output (Scatter Plot):
4. Employee Salary Distribution Histogram (`kind='hist'`)
Examining the frequency distribution of employee salary tiers across an organization.
Python (plot_histogram.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_salary_distribution():
data = {'Salary': [55, 62, 70, 72, 80, 85, 90, 95, 110, 125]}
df = pd.DataFrame(data)
df.plot(kind='hist', bins=5, color='#16834d', edgecolor='black', title='Salary Distribution Histogram')
plt.xlabel('Salary Range ($1k)')
plt.savefig('plot_hist.png')
plot_salary_distribution()
Rendered Plot Output (Histogram):
5. Expense Category Breakdown Pie Chart (`kind='pie'`)
Visualizing monthly household budget allocation across different expense categories.
Python (plot_pie.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_expense_pie():
data = {'Expenses': [1200, 450, 300, 200]}, index=['Rent', 'Food', 'Utilities', 'Transport']
df = pd.DataFrame(data)
df.plot(kind='pie', y='Expenses', autopct='%1.1f%%', title='Monthly Expense Share', colors=['#8a3028', '#c86d18', '#0ea5e9', '#6c2bd9'])
plt.savefig('plot_pie.png')
plot_expense_pie()
Rendered Plot Output (Pie Chart):
6. Multi-Series Trend Comparison (`kind='line'`)
Comparing annual revenue trajectories of two competing products across fiscal quarters.
Python (plot_multi_line.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_multi_trend():
data = {'Qtr': ['Q1', 'Q2', 'Q3', 'Q4'], 'Product_A': [100, 120, 140, 180], 'Product_B': [90, 110, 130, 160]}
df = pd.DataFrame(data).set_index('Qtr')
df.plot(kind='line', marker='s', title='Product Revenue Comparison')
plt.ylabel('Revenue ($k)')
plt.savefig('plot_multi_line.png')
plot_multi_trend()
Rendered Plot Output (Multi-Line Chart):
7. Stacked Bar Chart for Channel Conversions (`kind='bar', stacked=True`)
Visualizing total website conversion traffic stacked by marketing source across months.
Python (plot_stacked_bar.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_stacked_bar():
data = {'Month': ['Jan', 'Feb', 'Mar'], 'Organic': [300, 400, 450], 'Paid': [150, 200, 250]}
df = pd.DataFrame(data).set_index('Month')
df.plot(kind='bar', stacked=True, color=['#0ea5e9', '#c86d18'], title='Stacked Traffic Sources')
plt.savefig('plot_stacked.png')
plot_stacked_bar()
Rendered Plot Output (Stacked Bar Chart):
8. Horizontal Bar Chart for Product Rankings (`kind='barh'`)
Displaying top-selling products using horizontal bar charts for clean text label readability.
Python (plot_barh.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_horizontal_bar():
data = {'Product': ['Laptop', 'Monitor', 'Keyboard', 'Mouse'], 'Units': [120, 250, 400, 550]}
df = pd.DataFrame(data).set_index('Product')
df.plot(kind='barh', color='#0ea5e9', title='Top Selling Products')
plt.xlabel('Units Sold')
plt.savefig('plot_barh.png')
plot_horizontal_bar()
Rendered Plot Output (Horizontal Bar Chart):
9. Area Plot for Cumulative Growth (`kind='area'`)
Visualizing cumulative user signups over time using filled area charts.
Python (plot_area.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_area_chart():
data = {'Month': ['Jan', 'Feb', 'Mar', 'Apr'], 'Users': [1000, 2500, 4800, 8000]}
df = pd.DataFrame(data).set_index('Month')
df.plot(kind='area', color='#6c2bd9', alpha=0.4, title='Cumulative User Growth')
plt.savefig('plot_area.png')
plot_area_chart()
Rendered Plot Output (Area Chart):
10. Box Plot for Outlier Detection (`kind='box'`)
Identifying statistical median, quartiles, and outliers in property prices using box plots.
Python (plot_box.py)
import pandas as pd
import matplotlib.pyplot as plt
def plot_box_chart():
data = {'Property_Prices_k': [250, 280, 310, 340, 360, 400, 750]}
df = pd.DataFrame(data)
df.plot(kind='box', color='#087f69', title='Property Price Outlier Detection')
plt.savefig('plot_box.png')
plot_box_chart()
Rendered Plot Output (Box Plot):