Matplotlib with Pandas: 12
Visualization Examples
1. Line Plot - Stock Price Trend
import pandas as pd
import [Link] as plt
# Create sample data
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='M')
stock_prices = [100, 112, 108, 115, 135, 142, 138, 145, 152, 147, 158, 165]
df = [Link]({'Date': dates, 'Price': stock_prices})
[Link](figsize=(10, 6))
[Link](df['Date'], df['Price'], marker='o')
[Link]('Stock Price Trend 2023')
[Link]('Date')
[Link]('Price ($)')
[Link](True)
[Link]()
2. Bar Chart - Monthly Sales
# Sample sales data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [12500, 14800, 13900, 15200, 16400]
df = [Link]({'Month': months, 'Sales': sales})
[Link](figsize=(8, 6))
[Link](kind='bar', x='Month', y='Sales')
[Link]('Monthly Sales Performance')
[Link]('Month')
[Link]('Sales ($)')
[Link]()
3. Scatter Plot - Height vs Weight
# Generate sample data
[Link](42)
n = 50
height = [Link](170, 10, n)
weight = height * 0.7 + [Link](0, 5, n)
df = [Link]({'Height': height, 'Weight': weight})
[Link](figsize=(8, 6))
[Link](df['Height'], df['Weight'])
[Link]('Height vs Weight Correlation')
[Link]('Height (cm)')
[Link]('Weight (kg)')
[Link]()
4. Histogram - Age Distribution
# Generate age data
ages = [Link](35, 10, 1000)
df = [Link]({'Age': ages})
[Link](figsize=(8, 6))
df['Age'].hist(bins=30, edgecolor='black')
[Link]('Age Distribution')
[Link]('Age')
[Link]('Frequency')
[Link]()
5. Box Plot - Product Price Distribution
# Sample data for different products
products = {
'Product A': [Link](100, 15, 50),
'Product B': [Link](85, 12, 50),
'Product C': [Link](120, 20, 50)
}
df = [Link](products)
[Link](figsize=(8, 6))
[Link]()
[Link]('Product Price Distribution')
[Link]('Price ($)')
[Link]()
6. Pie Chart - Market Share
companies = ['Company A', 'Company B', 'Company C', 'Company D']
market_share = [35, 25, 22, 18]
df = [Link]({'Company': companies, 'Share': market_share})
[Link](figsize=(8, 8))
[Link](df['Share'], labels=df['Company'], autopct='%1.1f%%')
[Link]('Market Share Distribution')
[Link]()
7. Area Plot - Revenue Streams
dates = pd.date_range('2023-01-01', periods=12, freq='M')
data = {
'Product': [Link](50, 100, 12),
'Services': [Link](30, 70, 12),
'Consulting': [Link](20, 50, 12)
}
df = [Link](data, index=dates)
[Link](figsize=(10, 6))
[Link](kind='area', stacked=True)
[Link]('Revenue Streams Over Time')
[Link]('Date')
[Link]('Revenue ($K)')
[Link]()
8. Multiple Line Plot - Temperature
Comparison
dates = pd.date_range('2023-01-01', periods=12, freq='M')
data = {
'City A': [Link](15, 30, 12),
'City B': [Link](10, 25, 12),
'City C': [Link](20, 35, 12)
}
df = [Link](data, index=dates)
[Link](figsize=(10, 6))
[Link](marker='o')
[Link]('Temperature Comparison Across Cities')
[Link]('Date')
[Link]('Temperature (°C)')
[Link](True)
[Link]()
9. Grouped Bar Chart - Sales by Category
categories = ['Electronics', 'Clothing', 'Food']
data = {
'2022': [850, 730, 620],
'2023': [920, 780, 690]
}
df = [Link](data, index=categories)
ax = [Link](kind='bar', figsize=(8, 6), width=0.8)
[Link]('Sales by Category')
[Link]('Category')
[Link]('Sales ($K)')
[Link](title='Year')
[Link]()
10. Subplots - Multiple Visualizations
# Generate sample data
[Link](42)
data = {
'Sales': [Link](1000, 100, 12),
'Profit': [Link](200, 30, 12)
}
df = [Link](data, index=pd.date_range('2023-01-01', periods=12, freq='M'))
fig, (ax1, ax2) = [Link](2, 1, figsize=(10, 8))
# Plot 1: Sales Line Plot
df['Sales'].plot(ax=ax1, marker='o')
ax1.set_title('Monthly Sales')
ax1.set_ylabel('Sales ($)')
[Link](True)
# Plot 2: Profit Bar Plot
df['Profit'].plot(kind='bar', ax=ax2)
ax2.set_title('Monthly Profit')
ax2.set_ylabel('Profit ($)')
plt.tight_layout()
[Link]()
11. Heatmap - Correlation Matrix
# Generate correlated data
[Link](42)
n = 100
data = {
'A': [Link](0, 1, n),
'B': [Link](0, 1, n),
'C': [Link](0, 1, n),
'D': [Link](0, 1, n)
}
df = [Link](data)
correlation = [Link]()
[Link](figsize=(8, 6))
[Link](correlation, cmap='coolwarm', aspect='auto')
[Link]()
[Link](range(len([Link])), [Link])
[Link](range(len([Link])), [Link])
[Link]('Correlation Heatmap')
[Link]()
12. Time Series Decomposition
# Generate time series data
dates = pd.date_range('2023-01-01', periods=365, freq='D')
ts = [Link]([Link](100, 10, 365) + \
[Link]([Link](0, 4*[Link], 365)) * 20, index=dates)
# Plot components
fig, (ax1, ax2, ax3) = [Link](3, 1, figsize=(12, 10))
# Original
[Link](ax=ax1)
ax1.set_title('Original Time Series')
# 7-day rolling mean
[Link](window=7).mean().plot(ax=ax2)
ax2.set_title('7-Day Rolling Mean')
# 30-day rolling mean
[Link](window=30).mean().plot(ax=ax3)
ax3.set_title('30-Day Rolling Mean')
plt.tight_layout()
[Link]()
Each example includes:
Data generation/preparation using pandas
Matplotlib visualization code
Customization of plot appearance
Clear titles and labels
To run these examples, make sure you have the following imports:
import pandas as pd
import [Link] as plt
import numpy as np