Colors and Colormaps

Loading concept...

🎨 The Art of Colors in Matplotlib

Your Palette Awaits!

Imagine you’re a painter, but instead of canvas and brushes, you have data and code. Matplotlib is your art studio, and today we’re learning how to pick the perfect colors for your masterpieces!


🌈 Color Specification Methods

What is it?

Just like crayons have names (“red”, “blue”), Matplotlib lets you tell it what color you want in many different ways!

The 5 Ways to Say “I Want This Color”

1. Named Colors - Just say the name!

plt.plot(x, y, color='red')
plt.plot(x, y, color='tomato')
plt.plot(x, y, color='skyblue')

Simple! Like asking for “the red crayon.”

2. Hex Codes - The secret color language

plt.plot(x, y, color='#FF5733')
plt.plot(x, y, color='#3498DB')

It’s like a color password: #FF5733 = a nice orange!

3. RGB Tuples - Mix your own!

plt.plot(x, y, color=(1, 0, 0))    # Pure red
plt.plot(x, y, color=(0, 0.5, 1))  # Sky blue

Numbers go from 0 to 1. Think: (Red, Green, Blue)

4. RGBA Tuples - RGB + See-through magic!

plt.plot(x, y, color=(1, 0, 0, 0.5))

The 4th number is transparency (0.5 = half see-through)

5. Single Letters - Quick shortcuts

plt.plot(x, y, color='r')  # red
plt.plot(x, y, color='g')  # green
plt.plot(x, y, color='b')  # blue
plt.plot(x, y, color='k')  # black

Quick Reference

Method Example When to Use
Name 'coral' Easy & readable
Hex '#FF6B6B' Exact brand colors
RGB (0.2, 0.4, 0.8) Mixing custom colors
Letter 'r' Super quick plots

🔄 Color Cycling

The Story

Imagine drawing 5 lines. Do you want to pick 5 colors yourself? No way! Color cycling does it automatically!

How It Works

When you draw multiple lines, Matplotlib picks a different color for each one from a list. It’s like a color playlist that keeps repeating!

for i in range(4):
    plt.plot(x, y + i)
# Each line gets a new color automatically!

Customizing Your Color Playlist

# Use a different color cycle
plt.rcParams['axes.prop_cycle'] = plt.cycler(
    color=['#FF6B6B', '#4ECDC4', '#45B7D1']
)

Now your lines will go: coral → teal → blue → coral → …

graph TD A[Line 1] -->|Color 1| B[coral] C[Line 2] -->|Color 2| D[teal] E[Line 3] -->|Color 3| F[blue] G[Line 4] -->|Back to Color 1| H[coral]

🪟 Alpha Transparency

What is Alpha?

Remember tracing paper? You could see through it! Alpha makes your colors see-through like that.

The Magic Number

  • Alpha = 1 → Solid, can’t see through
  • Alpha = 0.5 → Half see-through
  • Alpha = 0 → Invisible!

Examples

# Half transparent red
plt.scatter(x, y, alpha=0.5, color='red')

# Using RGBA tuple
plt.bar(x, y, color=(0.2, 0.6, 1.0, 0.3))

Why Use Transparency?

  • Overlapping data: See both layers!
  • Density: Dark areas = more points
  • Beautiful effects: Soft, artistic look
graph TD A[Alpha = 1.0] -->|Solid| B[Cannot see behind] C[Alpha = 0.5] -->|Semi-transparent| D[Partially visible] E[Alpha = 0.0] -->|Invisible| F[Completely hidden]

🗺️ Colormap Types

The Big Idea

A colormap is like a rainbow ruler. Give it a number, and it gives you a color!

The 4 Colormap Families

1. 🌊 Sequential - Light to Dark

Perfect for: “How much?” questions

plt.imshow(data, cmap='Blues')
plt.imshow(data, cmap='viridis')

Low values → light | High values → dark

2. ↔️ Diverging - Two directions from center

Perfect for: “Above or below average?”

plt.imshow(data, cmap='RdBu')
plt.imshow(data, cmap='coolwarm')

Negative → blue | Zero → white | Positive → red

3. 🎯 Qualitative - Different categories

Perfect for: “What type?”

plt.scatter(x, y, c=categories, cmap='Set1')
plt.scatter(x, y, c=categories, cmap='tab10')

Each category gets a distinct color.

4. 🔁 Cyclic - Loops back around

Perfect for: Angles, time of day

plt.imshow(angles, cmap='twilight')
plt.imshow(hours, cmap='hsv')

End color matches start color!

graph TD A[Colormap Types] --> B[Sequential] A --> C[Diverging] A --> D[Qualitative] A --> E[Cyclic] B --> B1[viridis, Blues] C --> C1[RdBu, coolwarm] D --> D1[Set1, tab10] E --> E1[twilight, hsv]

📏 Color Normalization

The Problem

Your data goes from 0 to 1000, but colors go from 0 to 1. How do we connect them?

The Solution: Normalize!

Normalization squishes your data range into color range.

Types of Normalization

1. Linear (Default)

Maps values evenly.

from matplotlib.colors import Normalize
norm = Normalize(vmin=0, vmax=100)
plt.scatter(x, y, c=values, norm=norm)

2. Logarithmic

For data that spans huge ranges.

from matplotlib.colors import LogNorm
norm = LogNorm(vmin=1, vmax=1000)
plt.imshow(data, norm=norm)

3. Centered

For diverging data around a center point.

from matplotlib.colors import TwoSlopeNorm
norm = TwoSlopeNorm(vmin=-10, vcenter=0, vmax=50)

Visual Example

graph LR A[Data: 0-1000] -->|Normalize| B[Color: 0.0-1.0] B --> C[Pick color from colormap]

📊 Colorbar Basics

What is a Colorbar?

It’s the legend for colors! It tells you “this color means this value.”

Adding a Colorbar

img = plt.imshow(data, cmap='viridis')
plt.colorbar(img)

That’s it! One line of code!

What It Shows

  • The colormap stretched out
  • Numbers showing what values the colors represent
graph TD A[Your Plot] --> B[Add colorbar] B --> C[Users understand colors!] C --> D[Low values = dark] C --> E[High values = bright]

✨ Colorbar Customization

Make It Yours!

The basic colorbar works, but you can make it perfect.

Change the Label

cbar = plt.colorbar(img)
cbar.set_label('Temperature (°C)')

Change the Position

plt.colorbar(img, orientation='horizontal')
plt.colorbar(img, location='left')

Set Specific Tick Values

cbar = plt.colorbar(img)
cbar.set_ticks([0, 25, 50, 75, 100])

Shrink or Extend

plt.colorbar(img, shrink=0.8)  # 80% of height
plt.colorbar(img, extend='both')  # arrows on ends

Complete Example

fig, ax = plt.subplots()
img = ax.imshow(data, cmap='coolwarm')

cbar = plt.colorbar(img, shrink=0.8)
cbar.set_label('Score', fontsize=12)
cbar.set_ticks([0, 50, 100])
graph TD A[Basic Colorbar] --> B[Add Label] B --> C[Set Position] C --> D[Custom Ticks] D --> E[Perfect Colorbar!]

🎯 Quick Summary

Concept What It Does Key Code
Color Specs Define colors many ways color='#FF6B6B'
Color Cycling Auto-colors for lines Happens automatically
Alpha Make things see-through alpha=0.5
Colormaps Color gradients for data cmap='viridis'
Normalization Map data to colors norm=Normalize()
Colorbar Legend for colors plt.colorbar()
Customize Make colorbar perfect cbar.set_label()

🚀 You Did It!

You now know how to paint with data! Colors aren’t just pretty—they tell stories. A good color choice helps people understand your data instantly.

Remember: The best visualizations are both beautiful and clear!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.