Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Matplotlib Pandas Guide (1)

The document provides 12 visualization examples using Matplotlib with Pandas, including line plots, bar charts, scatter plots, and more. Each example includes sample data creation, plotting code, and a brief description of the output visualization. The visualizations cover various topics such as stock prices, sales performance, height vs weight correlation, and time series decomposition.

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)
2 views

Matplotlib Pandas Guide (1)

The document provides 12 visualization examples using Matplotlib with Pandas, including line plots, bar charts, scatter plots, and more. Each example includes sample data creation, plotting code, and a brief description of the output visualization. The visualizations cover various topics such as stock prices, sales performance, height vs weight correlation, and time series decomposition.

Uploaded by

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

Matplotlib with Pandas: 12

Visualization Examples with


Outputs
1. Line Plot - Stock Price Trend
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# 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()

Line plot showing increasing


stock price trend over 2023

Output:

2. Bar Chart - Monthly Sales


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()

Bar chart showing monthly sales


data

Output:

3. Scatter Plot - Height vs Weight


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()
Scatter plot showing correlation
between height and weight

Output:

4. Histogram - Age Distribution


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()

Histogram showing age


distribution

Output:

5. Box Plot - Product Price Distribution


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()

Box plot showing price


distribution for different products

Output:

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()
Pie chart showing market share
distribution

Output:

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()

Area plot showing stacked


revenue streams

Output:

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()

Multiple line plot comparing


temperatures

Output:

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()
Grouped bar chart comparing
sales across categories

Output:

10. Subplots - Multiple Visualizations


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))

df['Sales'].plot(ax=ax1, marker='o')
ax1.set_title('Monthly Sales')
ax1.set_ylabel('Sales ($)')
ax1.grid(True)

df['Profit'].plot(kind='bar', ax=ax2)
ax2.set_title('Monthly Profit')
ax2.set_ylabel('Profit ($)')

plt.tight_layout()
plt.show()

Subplots showing sales and profit


data

Output:
11. Heatmap - Correlation Matrix
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()

Heatmap showing correlation


matrix

Output:

12. Time Series Decomposition


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)

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

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

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

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

plt.tight_layout()
plt.show()

Time series decomposition with


original and rolling means

Output:

Note: The actual outputs will appear when you run the code in your environment. The placeholder images are used to
indicate where the visualizations would appear.

You might also like