Using Color

Color maps

A color map is a systematic mapping between data values and specific colors, often used in visualizations to represent data distribution, patterns, or intensity. It provides a way to encode numerical, categorical, or sequential data into a visual spectrum, making it easier for viewers to interpret complex datasets. For example, in a heatmap, a color map assigns shades ranging from cool colors (like blue) to warm colors (like red) to represent low and high values, respectively. This transformation of data into color gradients helps highlight variations, trends, or clusters in the dataset. The choice of a color map can significantly impact how effectively the data communicates its underlying insights.

Color maps can be customized to suit different visualization needs. Sequential color maps are often used for continuous data with an inherent order (e.g., temperature or sales trends), while diverging color maps are ideal for data centered around a midpoint, such as profit and loss. Qualitative color maps, on the other hand, are designed for categorical data where each distinct color represents a separate category. Tools like Plotly, Matplotlib, and Seaborn provide predefined color maps, and many allow for customization to align with specific themes or accessibility standards. By carefully selecting an appropriate color map, data analysts can ensure that their visualizations are both aesthetically pleasing and functionally informative.

For Plotly, you can use discrete color map to map categorical data to color hues.


Using color maps

Bar charts are versatile tools for visualizing comparisons between categories. Plotly provides a simple and powerful way to create various types of bar charts.

Simple Bar Chart

import pandas as pd
import plotly.express as px

# Sample data
data = {
    'Category': ['A', 'B', 'C', 'D'],
    'Values': [10, 20, 15, 25]
}

df = pd.DataFrame(data)

# Create a bar chart
fig = px.bar(df, x='Category', y='Values', title='Simple Bar Chart')
fig.show()

Using plotly.express with color_discrete_map

You can map specific values to custom colors by using the color_discrete_map parameter.

Example: Customizing Bar Colors by Category

import pandas as pd
import plotly.express as px

# Data
data = {
    'Category': ['A', 'B', 'C', 'D'],
    'Values': [10, 20, 15, 25]
}
df = pd.DataFrame(data)

# Define custom color mapping
custom_colors = {
    'A': 'blue',
    'B': 'green',
    'C': 'red',
    'D': 'purple'
}

# Create bar chart with custom colors
fig = px.bar(df, x='Category', y='Values', color='Category', 
             color_discrete_map=custom_colors, title='Custom Colors by Category')
fig.show()

Using color with plotly.express

You can also use the color field to modify the color for a specific subcategory or column.

import pandas as pd
import plotly.express as px

# Sample data
data = {
    'Category': ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D'],
    'Subcategory': ['A1', 'B1', 'C1', 'D1', 'A2', 'B2', 'C2', 'D2'],
    'Values': [10, 20, 15, 25, 11, 19, 19, 31]
}

df = pd.DataFrame(data)

# Create a bar chart
fig = px.scatter(df, x='Category', y='Values', color='Subcategory', title='Simple Bar Chart')

# Show the chart
fig.show()

Customizing Other Plot Types

You can use the same principles for scatter plots, line charts, and other Plotly visualizations. Specify the marker_coloror line_color parameter in graph_objects or use color_discrete_map with express.

Last updated