Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/colors/python$ _

Generate Random Colors in Python - Hex, RGB & HSL Examples

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Python

What: Generate random colors in Python using hex, RGB, and HSL formats for data visualization, matplotlib plots, and web applications.

Try it: Use our interactive Colors generator or integrate this code into your Python application.

Generate random colors in Python using hex, RGB, and HSL formats for data visualization, matplotlib plots, and web applications. Looking for other languages? Check our code examples in PHP , JavaScript , Java , C# , C++ , Ruby and Go or use our interactive web generator.

Python Code Example

import random

# Generate Random Hex Color
def random_hex_color():
    return f"#{random.randint(0, 0xFFFFFF):06x}"

print(f"Hex Color: {random_hex_color()}")

# Generate Random RGB Color
def random_rgb_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    return f"rgb({r}, {g}, {b})"

print(f"RGB Color: {random_rgb_color()}")

# Generate Random HSL Color
def random_hsl_color():
    h = random.randint(0, 360)
    s = random.randint(0, 100)
    l = random.randint(0, 100)
    return f"hsl({h}, {s}%, {l}%)"

print(f"HSL Color: {random_hsl_color()}")

# Generate Pastel Color
def random_pastel_color():
    h = random.randint(0, 360)
    s = random.randint(25, 100)
    l = random.randint(70, 90)
    return f"hsl({h}, {s}%, {l}%)"

print(f"Pastel Color: {random_pastel_color()}")

# Generate Color Palette (list of hex colors)
def generate_color_palette(count):
    return [random_hex_color() for _ in range(count)]

print(f"Color Palette: {generate_color_palette(5)}")

# RGB Tuple for Matplotlib
def random_rgb_tuple():
    return (random.random(), random.random(), random.random())

print(f"Matplotlib RGB: {random_rgb_tuple()}")

# RGB to Hex Conversion
def rgb_to_hex(r, g, b):
    return f"#{r:02x}{g:02x}{b:02x}"

print(f"RGB to Hex: {rgb_to_hex(255, 99, 71)}")

[EXPLANATION]

Python's random module generates random integers with randint(). For hex colors, use :06x format specifier to create a 6-digit hexadecimal string. RGB colors use three random integers (0-255) for each channel. HSL uses hue (0-360°), saturation (0-100%), and lightness (0-100%). For matplotlib compatibility, use random.random() to generate RGB tuples with values 0.0-1.0. The :02x format ensures 2-digit hex values with leading zeros.

Expected Output

Hex Color: #4a7c59
RGB Color: rgb(207, 142, 89)
HSL Color: hsl(318, 72%, 45%)
Pastel Color: hsl(203, 67%, 84%)
Color Palette: ['#2d5a8f', '#e85d75', '#ffa600', '#58b368', '#bc5090']
Matplotlib RGB: (0.7328428975265018, 0.2891647358533935, 0.9204859842875636)
RGB to Hex: #ff6347

Common Use Cases

  • Generate colors for matplotlib, seaborn, or plotly charts
  • Create color palettes for data visualization dashboards
  • Assign colors to categories in pandas DataFrames
  • Generate test data for web scraping or APIs
  • Create random backgrounds for Pillow image generation

Important Notes

  • random.randint() includes both endpoints
  • Matplotlib expects RGB tuples with values 0.0-1.0
  • Use list comprehension for efficient color palette generation
  • Consider using seaborn.color_palette() for harmonious palettes
  • For reproducibility, set seed with random.seed()

Try Our Interactive Generator

Don't want to write code? Use our free web-based Colors generator with instant results.

TRY COLORS GENERATOR →