Skip to main content
Pandas & NumPy
CHAPTER 19 Beginner

Data Visualization with Pandas

Updated: May 18, 2026
5 min read

# CHAPTER 19

Data Visualization with Pandas

1. Chapter Introduction

Pandas integrates with Matplotlib to enable visualization directly from DataFrames — one-line charts, subplots, styled figures. This chapter covers the essential chart types every data analyst needs.

2. Setup

python
123456789
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

# Set global style
plt.style.use('seaborn-v0_8-whitegrid')
plt.rcParams['figure.figsize'] = (10, 6)
plt.rcParams['font.size'] = 12

3. Line Chart

python
12345678910111213141516171819202122232425262728
# Stock price simulation
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=252, freq='B')
returns = np.random.normal(0.0005, 0.02, 252)

df_stock = pd.DataFrame({
    'AAPL': 150 * np.cumprod(1 + returns),
    'GOOGL': 130 * np.cumprod(1 + np.random.normal(0.0008, 0.022, 252)),
    'MSFT': 370 * np.cumprod(1 + np.random.normal(0.0006, 0.018, 252))
}, index=dates)

# Line chart — one line
ax = df_stock['AAPL'].plot(title='AAPL Stock Price 2024',
                            ylabel='Price ($)',
                            xlabel='Date',
                            color='#2196F3',
                            linewidth=1.5)
plt.tight_layout()
plt.savefig('aapl_stock.png', dpi=150)
plt.show()

# Multiple lines
ax2 = df_stock.plot(title='Stock Comparison 2024',
                     ylabel='Price ($)',
                     figsize=(12, 6))
plt.tight_layout()
plt.savefig('stock_comparison.png', dpi=150)
plt.show()

4. Bar Chart

python
1234567891011121314151617181920212223242526272829303132333435363738
# Horizontal and vertical bar charts
dept_revenue = pd.Series({
    'Engineering': 245000,
    'Marketing': 128000,
    'Sales': 312000,
    'HR': 85000,
    'Finance': 167000
}).sort_values(ascending=True)

fig, axes = plt.subplots(1, 2, figsize=(14, 5))

# Vertical bar
dept_revenue.sort_values(ascending=False).plot(
    kind='bar', ax=axes[0], color='#4CAF50', edgecolor='white', title='Revenue by Dept')
axes[0].set_ylabel('Revenue ($)')
axes[0].tick_params(axis='x', rotation=30)

# Horizontal bar
dept_revenue.plot(kind='barh', ax=axes[1], color='#2196F3', edgecolor='white',
                  title='Revenue by Dept (Horizontal)')
axes[1].set_xlabel('Revenue ($)')

plt.tight_layout()
plt.savefig('bar_charts.png', dpi=150)
plt.show()

# Grouped bar chart
monthly_data = pd.DataFrame({
    'Product_A': [42000, 48000, 55000, 51000],
    'Product_B': [35000, 38000, 42000, 40000],
    'Product_C': [28000, 32000, 30000, 35000]
}, index=['Q1', 'Q2', 'Q3', 'Q4'])

monthly_data.plot(kind='bar', figsize=(10, 6), title='Quarterly Revenue by Product')
plt.ylabel('Revenue ($)')
plt.xticks(rotation=0)
plt.savefig('grouped_bar.png', dpi=150)
plt.show()

5. Histogram and Distribution

python
123456789101112131415161718192021
# Salary distribution
np.random.seed(42)
salaries = pd.DataFrame({
    'Engineering': np.random.normal(95000, 15000, 200),
    'Marketing': np.random.normal(72000, 12000, 200),
    'Sales': np.random.normal(65000, 18000, 200)
})

fig, axes = plt.subplots(1, 2, figsize=(14, 5))

# Histogram
salaries.plot(kind='hist', bins=30, alpha=0.6, ax=axes[0], title='Salary Distribution')
axes[0].set_xlabel('Salary ($)')

# Density (KDE)
salaries.plot(kind='kde', ax=axes[1], title='Salary Density', linewidth=2)
axes[1].set_xlabel('Salary ($)')

plt.tight_layout()
plt.savefig('histogram.png', dpi=150)
plt.show()

6. Scatter Plot and Box Plot

python
12345678910111213141516171819
# Scatter plot — correlation visualization
df = pd.DataFrame({
    'Experience': np.random.randint(1, 20, 100),
    'Salary': np.random.normal(60000, 20000, 100)
})
df['Salary'] = df['Salary'] + df['Experience'] * 3000  # Add correlation

fig, axes = plt.subplots(1, 2, figsize=(14, 5))

df.plot.scatter(x='Experience', y='Salary', alpha=0.6, ax=axes[0],
                title='Experience vs Salary', color='#9C27B0')

# Box plot — distribution comparison
salaries.plot(kind='box', ax=axes[1], title='Salary Distribution by Dept')
axes[1].set_ylabel('Salary ($)')

plt.tight_layout()
plt.savefig('scatter_box.png', dpi=150)
plt.show()

7. Common Mistakes

  • plt.show() before savefig(): Always savefig() before show()show() clears the figure.
  • Not using tight_layout(): Labels and titles often overlap. Always call plt.tight_layout() before saving.

8. MCQs

Question 1

df.plot(kind='bar') creates?

Question 2

kind='barh' creates?

Question 3

kind='hist', bins=30 controls?

Question 4

kind='kde' shows?

Question 5

alpha=0.6 in plots?

Question 6

plt.style.use('seaborn-v0_8-whitegrid') applies?

Question 7

savefig() must be called?

Question 8

Box plot shows?

Question 9

Scatter plot is best for visualizing?

Question 10

plt.tight_layout() fixes?

9. Interview Questions

  • Q: What chart type would you use to show the relationship between experience and salary?
  • Q: How do you save a Pandas plot to a file?

10. Summary

Pandas .plot() wraps Matplotlib for quick charts. Key kind values: line, bar, barh, hist, kde, scatter, box. Always tight_layout() before savefig(). Use plt.style.use() for consistent aesthetics. For advanced plots, use Seaborn or Plotly.

11. Next Chapter Recommendation

In Chapter 20: Advanced NumPy Concepts, we explore advanced indexing, structured arrays, memory optimization, and performance tuning.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·