Skip to main content
Data Visualization
CHAPTER 11 Beginner

Working with Colors, Themes, and Styles

Updated: May 18, 2026
5 min read

# CHAPTER 11

Working with Colors, Themes, and Styles

1. Chapter Introduction

Color is the most emotionally powerful element of data visualization. Misused, it confuses and misleads. Mastered, it guides attention, encodes meaning, and creates professional, accessible charts that communicate instantly.

2. Color Palettes in Practice

python
12345678910111213141516171819202122232425262728293031323334
import matplotlib.pyplot as plt
import numpy as np

# Sequential palettes (for ordered/magnitude data)
fig, axes = plt.subplots(2, 3, figsize=(15, 8))

# Qualitative palettes
qualitative = {
    'tab10':     plt.cm.tab10,
    'Set2':      plt.cm.Set2,
    'Paired':    plt.cm.Paired,
}
for ax, (name, cmap) in zip(axes[0], qualitative.items()):
    colors = [cmap(i) for i in np.linspace(0, 0.9, 8)]
    ax.bar(range(8), [1]*8, color=colors)
    ax.set_title(f'{name}\n(Categorical)', fontsize=11)
    ax.set_xticks([])

# Sequential palettes
sequential = {
    'Blues':     plt.cm.Blues,
    'YlOrRd':    plt.cm.YlOrRd,
    'viridis':   plt.cm.viridis,
}
for ax, (name, cmap) in zip(axes[1], sequential.items()):
    colors = [cmap(i) for i in np.linspace(0.2, 1.0, 8)]
    ax.bar(range(8), [1]*8, color=colors)
    ax.set_title(f'{name}\n(Sequential)', fontsize=11)
    ax.set_xticks([])

plt.suptitle('Color Palette Reference', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig('color_palettes.png', dpi=150)
plt.show()

3. Matplotlib Styles

python
123456789101112131415161718192021
import matplotlib.pyplot as plt
import numpy as np

styles = ['default', 'seaborn-v0_8-whitegrid', 'ggplot', 'dark_background', 'bmh']
x = np.linspace(0, 10, 100)

fig, axes = plt.subplots(1, 5, figsize=(20, 4))
for ax, style in zip(axes, styles):
    with plt.style.context(style):
        ax.plot(x, np.sin(x), linewidth=2)
        ax.plot(x, np.cos(x), linewidth=2)
        ax.set_title(style.replace('seaborn-v0_8-', 'sns-'), fontsize=9)
        ax.set_xticks([])

plt.suptitle('Matplotlib Style Comparison', fontsize=13, fontweight='bold')
plt.tight_layout()
plt.savefig('styles.png', dpi=150)
plt.show()

# Apply globally for all charts in session
plt.style.use('seaborn-v0_8-whitegrid')

4. Professional Color System

python
1234567891011121314151617181920212223242526272829303132
# Design system: consistent brand colors
BRAND_COLORS = {
    'primary':    '#1565C0',  # Deep blue — main data
    'secondary':  '#2E7D32',  # Deep green — positive/growth
    'danger':     '#C62828',  # Deep red — negative/alert
    'warning':    '#E65100',  # Orange — caution
    'neutral':    '#546E7A',  # Blue-grey — background data
    'highlight':  '#FFD54F',  # Amber — emphasis/callout
}

# Accessibility: color-blind safe palette (ColorBrewer)
COLORBLIND_SAFE = ['#E69F00', '#56B4E9', '#009E73', '#F0E442', '#0072B2', '#D55E00', '#CC79A7']

# Apply to a chart
fig, ax = plt.subplots(figsize=(10, 5))
depts = ['Engineering', 'Marketing', 'Sales', 'HR', 'Finance']
values = [2.8, 1.9, 2.3, 0.8, 1.5]

bars = ax.bar(depts, values, color=[BRAND_COLORS['primary'] if v == max(values)
              else BRAND_COLORS['neutral'] for v in values])
for bar, val in zip(bars, values):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.03,
            f'${val:.1f}M', ha='center', fontweight='bold')

ax.set_title('Revenue by Department', fontsize=14, fontweight='bold')
ax.set_ylabel('Revenue (Million $)')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.grid(True, axis='y', alpha=0.3)
plt.tight_layout()
plt.savefig('brand_colors.png', dpi=150)
plt.show()

5. Common Mistakes

  • Rainbow colormaps for continuous data: Rainbow (jet) has perceptual issues — the middle yellows look brighter than endpoints. Use viridis, plasma, or Blues instead.
  • Using color as the only difference: ~8% of men are color-blind. Always add shape, pattern, or label differentiation alongside color.

6. MCQs

Question 1

Sequential color palette is used for?

Question 2

Why avoid the jet (rainbow) colormap?

Question 3

plt.style.use('ggplot') applies?

Question 4

Colorblind-safe design rule?

Question 5

Diverging palette is best when?

Question 6

with plt.style.context(style): applies style?

Question 7

viridis advantage over jet?

Question 8

Qualitative palette is for?

Question 9

Brand color system improves?

Question 10

Tab10 provides?

7. Interview Questions

  • Q: Why is the jet colormap considered problematic in data visualization?
  • Q: How do you make charts accessible for color-blind viewers?

8. Summary

Color encoding: sequential for magnitude, diverging for +/- data, qualitative for categories. Use viridis/plasma for continuous data (perceptually uniform). Apply plt.style.use() for consistent aesthetics. Build a brand color system. Always add non-color differentiators for accessibility. Never use rainbow (jet).

9. Next Chapter Recommendation

In Chapter 12: Subplots and Multi-Chart Layouts, we create dashboard-style multi-panel figures using plt.subplots() and GridSpec.

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: ·