Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
Data Processing OverviewCSV Data HandlingPandas BasicsDataFramesData FilteringAggregation & GroupingData Cleaning & WranglingNumPy ArraysData Visualization Basics
Python/Data Processing/Data Visualization Basics

📈 Data Visualization Basics — Creating Meaningful Charts

Turn data into visual stories. Learn to create charts that communicate insights clearly.


🎯 Why Visualization?

Visual representation makes patterns and insights immediately obvious. A picture is worth a thousand data points.

import matplotlib.pyplot as plt
import pandas as pd

# Sample data
sales = [100, 150, 120, 200, 180, 210]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']

# Create line chart
plt.figure(figsize=(8, 5))
plt.plot(months, sales, marker='o', linewidth=2)
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.show()

📦 Installation and Setup

# Install matplotlib (if needed)
# pip install matplotlib

# Import pyplot
import matplotlib.pyplot as plt

# Jupyter notebook magic (if using Jupyter)
# %matplotlib inline

📊 Line Charts

Plot trends over time or categories.

import matplotlib.pyplot as plt

# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [100, 150, 120, 200, 180]

# Simple line chart
plt.plot(months, sales)
plt.title('Sales Trend')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.show()

# Multiple lines
plt.plot(months, sales, label='Sales', marker='o')
profits = [20, 30, 25, 40, 35]
plt.plot(months, profits, label='Profit', marker='s')
plt.legend()
plt.title('Sales vs Profit')
plt.show()

📊 Bar Charts

Compare values across categories.

import matplotlib.pyplot as plt

# Data
products = ['Laptop', 'Mouse', 'Keyboard', 'Monitor']
quantities = [50, 200, 150, 75]

# Bar chart
plt.bar(products, quantities, color='steelblue')
plt.title('Product Sales Volume')
plt.xlabel('Product')
plt.ylabel('Quantity Sold')
plt.xticks(rotation=45)
plt.show()

# Horizontal bars
plt.barh(products, quantities, color='coral')
plt.title('Product Sales Volume')
plt.show()

# Grouped bars
import numpy as np
categories = ['Q1', 'Q2', 'Q3']
product1 = [20, 25, 30]
product2 = [15, 22, 28]

x = np.arange(len(categories))
width = 0.35

plt.bar(x - width/2, product1, width, label='Product A')
plt.bar(x + width/2, product2, width, label='Product B')
plt.xlabel('Quarter')
plt.ylabel('Sales')
plt.xticks(x, categories)
plt.legend()
plt.show()

🥧 Pie Charts

Show composition and proportions.

import matplotlib.pyplot as plt

# Data
categories = ['Sales', 'Marketing', 'Operations', 'R&D']
budgets = [400, 250, 150, 200]

# Pie chart
plt.pie(budgets, labels=categories, autopct='%1.1f%%')
plt.title('Budget Distribution')
plt.show()

# With exploded slice
explode = (0.05, 0, 0, 0)  # Separate Sales slice
plt.pie(budgets, labels=categories, autopct='%1.1f%%', explode=explode)
plt.title('Budget Distribution')
plt.show()

📈 Scatter Plots

Show relationships between two variables.

import matplotlib.pyplot as plt

# Data: age vs salary
ages = [25, 30, 28, 35, 32, 27, 31]
salaries = [50000, 65000, 55000, 75000, 70000, 52000, 62000]

# Scatter plot
plt.scatter(ages, salaries, s=100, alpha=0.6)
plt.title('Age vs Salary')
plt.xlabel('Age')
plt.ylabel('Salary ($)')
plt.grid(True, alpha=0.3)
plt.show()

# Multiple scatter plots
experience = [2, 5, 3, 8, 6, 1, 7]
colors = ['red' if x > 30 else 'blue' for x in ages]
plt.scatter(experience, salaries, c=colors, s=100, alpha=0.6)
plt.title('Experience vs Salary')
plt.xlabel('Years Experience')
plt.ylabel('Salary ($)')
plt.show()

📊 Histograms

Show distribution of data.

import matplotlib.pyplot as plt
import numpy as np

# Data: test scores
scores = [75, 82, 90, 65, 78, 88, 92, 70, 85, 95, 88, 80]

# Histogram
plt.hist(scores, bins=5, color='skyblue', edgecolor='black')
plt.title('Score Distribution')
plt.xlabel('Score')
plt.ylabel('Frequency')
plt.show()

# Multiple distributions
scores1 = [70, 75, 80, 85, 90]
scores2 = [65, 78, 88, 92, 95]
plt.hist(scores1, bins=5, alpha=0.5, label='Class A')
plt.hist(scores2, bins=5, alpha=0.5, label='Class B')
plt.legend()
plt.title('Score Distribution')
plt.show()

🎨 Real-World Example: Sales Dashboard

import matplotlib.pyplot as plt
import pandas as pd

# Sales data
data = {
    'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'product_a': [100, 120, 110, 150, 140, 160],
    'product_b': [80, 90, 95, 110, 120, 130],
    'product_c': [60, 70, 75, 85, 95, 100]
}

df = pd.DataFrame(data)

# Create subplots
fig, axes = plt.subplots(2, 2, figsize=(12, 10))

# Plot 1: Line chart - Trends
axes[0, 0].plot(df['month'], df['product_a'], label='Product A', marker='o')
axes[0, 0].plot(df['month'], df['product_b'], label='Product B', marker='s')
axes[0, 0].plot(df['month'], df['product_c'], label='Product C', marker='^')
axes[0, 0].set_title('Sales Trends')
axes[0, 0].legend()

# Plot 2: Bar chart - Comparison
axes[0, 1].bar(df['month'], df['product_a'], alpha=0.7, label='Product A')
axes[0, 1].set_title('Product A Sales')
axes[0, 1].set_ylabel('Sales')

# Plot 3: Average per product
avg_sales = [df['product_a'].mean(), df['product_b'].mean(), df['product_c'].mean()]
products = ['A', 'B', 'C']
axes[1, 0].bar(products, avg_sales, color=['blue', 'orange', 'green'])
axes[1, 0].set_title('Average Sales by Product')
axes[1, 0].set_ylabel('Average Sales')

# Plot 4: Total by product
total_sales = [df['product_a'].sum(), df['product_b'].sum(), df['product_c'].sum()]
axes[1, 1].pie(total_sales, labels=products, autopct='%1.1f%%')
axes[1, 1].set_title('Total Sales Distribution')

plt.tight_layout()
plt.show()

🎨 Customization

import matplotlib.pyplot as plt

data = [1, 2, 3, 4, 5]

# Styling
plt.figure(figsize=(10, 6))
plt.plot(data, linewidth=2, color='darkblue', marker='o', markersize=8)

# Titles and labels
plt.title('Example Chart', fontsize=16, fontweight='bold')
plt.xlabel('X Axis', fontsize=12)
plt.ylabel('Y Axis', fontsize=12)

# Grid and background
plt.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()

# Save figure
plt.savefig('chart.png', dpi=300, bbox_inches='tight')
plt.show()

📊 Chart Types Summary

ChartBest ForExample
LineTrends over timeStock prices
BarCategory comparisonSales by region
PieCompositionBudget distribution
ScatterRelationshipsAge vs salary
HistogramDistributionTest scores

🔑 Key Takeaways

  • ✅ Use line charts to show trends over time
  • ✅ Use bar charts to compare values across categories
  • ✅ Use pie charts to show composition (avoid many slices)
  • ✅ Use scatter plots to explore relationships
  • ✅ Use histograms to understand data distribution
  • ✅ Customize colors, sizes, and labels for clarity
  • ✅ Use subplots to display multiple charts together

Ready for advanced topics? Advanced Data Processing | Challenges


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn