0% found this document useful (0 votes)
3 views

Matplotlib Pandas Guide

The document provides 12 visualization examples using Matplotlib with Pandas, showcasing various types of plots such as line plots, bar charts, scatter plots, and more. Each example includes data generation, visualization code, and customization of plot appearance. The document serves as a guide for creating different visualizations and emphasizes the importance of clear titles and labels.

Uploaded by

harshitaupreti04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Matplotlib Pandas Guide

The document provides 12 visualization examples using Matplotlib with Pandas, showcasing various types of plots such as line plots, bar charts, scatter plots, and more. Each example includes data generation, visualization code, and customization of plot appearance. The document serves as a guide for creating different visualizations and emphasizes the importance of clear titles and labels.

Uploaded by

harshitaupreti04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Matplotlib with Pandas: 12

Visualization Examples
1. Line Plot - Stock Price Trend
import pandas as pd
import matplotlib.pyplot 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 = pd.DataFrame({'Date': dates, 'Price': stock_prices})

plt.figure(figsize=(10, 6))
plt.plot(df['Date'], df['Price'], marker='o')
plt.title('Stock Price Trend 2023')
plt.xlabel('Date')
plt.ylabel('Price ($)')
plt.grid(True)
plt.show()

2. Bar Chart - Monthly Sales


# Sample sales data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [12500, 14800, 13900, 15200, 16400]
df = pd.DataFrame({'Month': months, 'Sales': sales})

plt.figure(figsize=(8, 6))
df.plot(kind='bar', x='Month', y='Sales')
plt.title('Monthly Sales Performance')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.show()
3. Scatter Plot - Height vs Weight
# Generate sample data
np.random.seed(42)
n = 50
height = np.random.normal(170, 10, n)
weight = height * 0.7 + np.random.normal(0, 5, n)
df = pd.DataFrame({'Height': height, 'Weight': weight})

plt.figure(figsize=(8, 6))
plt.scatter(df['Height'], df['Weight'])
plt.title('Height vs Weight Correlation')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.show()

4. Histogram - Age Distribution


# Generate age data
ages = np.random.normal(35, 10, 1000)
df = pd.DataFrame({'Age': ages})

plt.figure(figsize=(8, 6))
df['Age'].hist(bins=30, edgecolor='black')
plt.title('Age Distribution')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()

5. Box Plot - Product Price Distribution


# Sample data for different products
products = {
'Product A': np.random.normal(100, 15, 50),
'Product B': np.random.normal(85, 12, 50),
'Product C': np.random.normal(120, 20, 50)
}
df = pd.DataFrame(products)

plt.figure(figsize=(8, 6))
df.boxplot()
plt.title('Product Price Distribution')
plt.ylabel('Price ($)')
plt.show()

6. Pie Chart - Market Share


companies = ['Company A', 'Company B', 'Company C', 'Company D']
market_share = [35, 25, 22, 18]
df = pd.DataFrame({'Company': companies, 'Share': market_share})

plt.figure(figsize=(8, 8))
plt.pie(df['Share'], labels=df['Company'], autopct='%1.1f%%')
plt.title('Market Share Distribution')
plt.show()

7. Area Plot - Revenue Streams


dates = pd.date_range('2023-01-01', periods=12, freq='M')
data = {
'Product': np.random.uniform(50, 100, 12),
'Services': np.random.uniform(30, 70, 12),
'Consulting': np.random.uniform(20, 50, 12)
}
df = pd.DataFrame(data, index=dates)

plt.figure(figsize=(10, 6))
df.plot(kind='area', stacked=True)
plt.title('Revenue Streams Over Time')
plt.xlabel('Date')
plt.ylabel('Revenue ($K)')
plt.show()

8. Multiple Line Plot - Temperature


Comparison
dates = pd.date_range('2023-01-01', periods=12, freq='M')
data = {
'City A': np.random.uniform(15, 30, 12),
'City B': np.random.uniform(10, 25, 12),
'City C': np.random.uniform(20, 35, 12)
}
df = pd.DataFrame(data, index=dates)

plt.figure(figsize=(10, 6))
df.plot(marker='o')
plt.title('Temperature Comparison Across Cities')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.grid(True)
plt.show()

9. Grouped Bar Chart - Sales by Category


categories = ['Electronics', 'Clothing', 'Food']
data = {
'2022': [850, 730, 620],
'2023': [920, 780, 690]
}
df = pd.DataFrame(data, index=categories)

ax = df.plot(kind='bar', figsize=(8, 6), width=0.8)


plt.title('Sales by Category')
plt.xlabel('Category')
plt.ylabel('Sales ($K)')
plt.legend(title='Year')
plt.show()

10. Subplots - Multiple Visualizations


# Generate sample data
np.random.seed(42)
data = {
'Sales': np.random.normal(1000, 100, 12),
'Profit': np.random.normal(200, 30, 12)
}
df = pd.DataFrame(data, index=pd.date_range('2023-01-01', periods=12, freq='M'))

fig, (ax1, ax2) = plt.subplots(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 ($)')
ax1.grid(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()
plt.show()
11. Heatmap - Correlation Matrix
# Generate correlated data
np.random.seed(42)
n = 100
data = {
'A': np.random.normal(0, 1, n),
'B': np.random.normal(0, 1, n),
'C': np.random.normal(0, 1, n),
'D': np.random.normal(0, 1, n)
}
df = pd.DataFrame(data)
correlation = df.corr()

plt.figure(figsize=(8, 6))
plt.imshow(correlation, cmap='coolwarm', aspect='auto')
plt.colorbar()
plt.xticks(range(len(correlation.columns)), correlation.columns)
plt.yticks(range(len(correlation.columns)), correlation.columns)
plt.title('Correlation Heatmap')
plt.show()

12. Time Series Decomposition


# Generate time series data
dates = pd.date_range('2023-01-01', periods=365, freq='D')
ts = pd.Series(np.random.normal(100, 10, 365) + \
np.sin(np.linspace(0, 4*np.pi, 365)) * 20, index=dates)

# Plot components
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(12, 10))

# Original
ts.plot(ax=ax1)
ax1.set_title('Original Time Series')

# 7-day rolling mean


ts.rolling(window=7).mean().plot(ax=ax2)
ax2.set_title('7-Day Rolling Mean')

# 30-day rolling mean


ts.rolling(window=30).mean().plot(ax=ax3)
ax3.set_title('30-Day Rolling Mean')

plt.tight_layout()
plt.show()

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 matplotlib.pyplot as plt
import numpy as np

You might also like