# Plotly Colors: Discrete Palettes, Color Maps, and Continuous Scales

> Change Plotly colors with color_discrete_map={"A": "#hex"} or color_discrete_sequence=px.colors.qualitative.Safe, and continuous colors with color_continuous_scale="Viridis". Covers graph_objects and default palettes.

Source: https://www.pythoncharts.com/plotly/plotly-colors/ | Published: 2026-12-05 | Author: Alex Scott | Category: Plotly | Tags: plotly, plotly express, colors, colorscale, color palette

In Plotly Express, set category colors with `color_discrete_map={"Category": "#hex"}` to choose each one, or `color_discrete_sequence=px.colors.qualitative.Safe` to switch the whole palette. For numeric colors, use `color_continuous_scale="Viridis"`. With `graph_objects`, set `marker_color=` or `line_color=` on the trace. The examples use Plotly 6.9 and also run on Plotly 7.

## Quick Example

```python
import numpy as np
import pandas as pd
import plotly.express as px

rng = np.random.default_rng(12)
weeks = pd.date_range("2026-01-05", periods=20, freq="W-MON")
installs = pd.DataFrame({
    "week": np.tile(weeks, 3),
    "platform": np.repeat(["iOS", "Android", "Web"], 20),
    "installs": np.concatenate([
        3200 + 40 * np.arange(20) + rng.normal(0, 150, 20),
        4100 + 90 * np.arange(20) + rng.normal(0, 200, 20),
        1500 - 20 * np.arange(20) + rng.normal(0, 120, 20),
    ]).round(),
})

fig = px.line(
    installs, x="week", y="installs", color="platform",
    color_discrete_map={"iOS": "#1d4ed8", "Android": "#16a34a", "Web": "#9ca3af"},
    labels={"week": "", "installs": "Weekly installs", "platform": "Platform"},
    title="App installs by platform",
)
fig.update_traces(line_width=3)
fig.show()
```

![Plotly line chart of weekly app installs over 20 weeks with Android in green rising fastest, iOS in blue rising more slowly, and Web in gray drifting down](https://www.pythoncharts.com/plotly/plotly-colors/images/plotly-colors-quick-example.png)

The dictionary keys are the values in the `platform` column, so each platform keeps its color however the data is sorted or filtered. Gray for Web pushes it into the background, which suits a chart about the two app stores. Colors can be hex codes, CSS names (`"steelblue"`), or `rgb()`/`rgba()` strings.

## Built-in color palettes for categories

`px.colors.qualitative` holds Plotly's palettes for categories. Each is a plain list of color strings, and `swatches()` draws them all:

```python
fig = px.colors.qualitative.swatches()
fig.update_layout(width=800, height=900)
fig.show()
```

![Plotly swatch chart of the built-in qualitative palettes, one row of colored blocks each for Plotly, D3, G10, T10, Alphabet, Dark24, Light24, Set1, Pastel1, Dark2, Set2, Pastel2, Set3, Antique, Bold, Pastel, Prism, Safe, and Vivid](https://www.pythoncharts.com/plotly/plotly-colors/images/plotly-colors-qualitative-swatches.png)

`Plotly` is the default. `Safe` is designed to stay distinguishable for colorblind readers, `Dark24` and `Light24` have 24 colors for many categories, and `Set2` and `Pastel` are softer. Add `_r` to reverse any of them (`px.colors.qualitative.Set2_r`).

## `color_discrete_sequence` vs `color_discrete_map`

`color_discrete_sequence=` hands out colors in order: the first category gets the first color, and so on. `color_discrete_map=` names the color for each category. Here are the same bars both ways:

```python
sales = pd.DataFrame({
    "region": ["North", "South", "East", "West", "Central"],
    "revenue": [4.2, 3.1, 5.6, 2.7, 3.8],
})

fig = px.bar(sales, x="region", y="revenue", color="region",
             color_discrete_sequence=px.colors.qualitative.Safe,
             labels={"revenue": "Revenue ($M)", "region": ""},
             title="color_discrete_sequence=px.colors.qualitative.Safe",
             width=700, height=380)
fig.update_layout(showlegend=False)
fig.show()

highlight = {region: "#cbd5e1" for region in sales["region"]}
highlight["East"] = "#ea580c"
fig = px.bar(sales, x="region", y="revenue", color="region",
             color_discrete_map=highlight,
             labels={"revenue": "Revenue ($M)", "region": ""},
             title="color_discrete_map: East highlighted",
             width=700, height=380)
fig.update_layout(showlegend=False)
fig.show()
```

![Plotly bar chart of revenue for five regions, each bar a different color from the Safe palette](https://www.pythoncharts.com/plotly/plotly-colors/images/plotly-colors-sequence.png)

![The same Plotly bar chart with every region in light gray except East, the highest at 5.6 million dollars, in orange](https://www.pythoncharts.com/plotly/plotly-colors/images/plotly-colors-map.png)

| Use | When |
|---|---|
| `color_discrete_sequence=` | You want a different palette and don't mind which category gets which color |
| `color_discrete_map=` | A category must always have the same color (brands, parties, "Other" in gray), or you're highlighting one |
| `color_discrete_map="identity"` | The column already holds color strings, such as a `"color"` column you built |
| `color_discrete_sequence=["#0f766e"]` | One color for every bar or line, without `color=` |

A sequence follows the order of the data, so filtering out one category shifts every color after it. When the same categories appear in several charts, a map keeps them consistent.

## Continuous colorscales for numbers

When `color=` is numeric, Plotly uses a continuous scale. `color_continuous_scale=` takes a scale name, `range_color=` fixes the ends, and for values above and below a center point, `color_continuous_midpoint=` centers a diverging scale:

```python
months = pd.period_range("2025-01", periods=24, freq="M").strftime("%b %Y")
anomaly = np.round(0.4 * np.sin(np.arange(24) / 3) + rng.normal(0, 0.35, 24), 2)
temps = pd.DataFrame({"month": months, "anomaly": anomaly})

fig = px.bar(temps, x="month", y="anomaly", color="anomaly",
             color_continuous_scale="RdBu_r", color_continuous_midpoint=0,
             labels={"anomaly": "°C vs normal", "month": ""},
             title="Monthly temperature anomaly", width=900, height=420)
fig.update_xaxes(tickangle=-45)
fig.show()
```

![Plotly bar chart of 24 monthly temperature anomalies colored on a red-blue diverging scale centered at zero, with warmer months in red above zero and cooler months in blue below](https://www.pythoncharts.com/plotly/plotly-colors/images/plotly-colors-diverging.png)

With `color_continuous_midpoint=0`, zero is always the white center of the scale, even when the data runs further above zero than below it (here from -0.61 to +0.67). The `_r` suffix reverses a scale, which puts red on the warm side here.

Scales come in three families:

| Family | Module | Examples | For |
|---|---|---|---|
| Sequential | `px.colors.sequential` | `"Viridis"`, `"Blues"`, `"Plasma"`, `"YlOrRd"` | Low to high (counts, prices) |
| Diverging | `px.colors.diverging` | `"RdBu"`, `"PiYG"`, `"Tealrose"` | Above and below a center (change, anomalies) |
| Cyclical | `px.colors.cyclical` | `"Twilight"`, `"IceFire"`, `"Phase"` | Values that wrap around (hours, angles, months) |

`px.colors.named_colorscales()` lists all 94 names, and `px.colors.sequential.swatches_continuous()` draws the sequential scales as gradients. Names are case-insensitive. A misspelled name raises `ValueError: Invalid value of type 'builtins.str' received for the 'colorscale' property`.

## Make your own colorscale

Pass a list of colors to spread them evenly, or `[position, color]` pairs to place them:

```python
grid = pd.DataFrame(rng.gamma(2, 15, (6, 10)).round(),
                    index=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
                    columns=[f"{h}:00" for h in range(9, 19)])

fig = px.imshow(
    grid, text_auto=True, aspect="auto",
    color_continuous_scale=[[0, "#f8fafc"], [0.5, "#5eead4"], [1, "#134e4a"]],
    labels={"color": "Visitors"}, title="Visitors by day and hour",
    width=800, height=380,
)
fig.show()
```

![Plotly heatmap of visitors by weekday and hour on a custom scale from near-white through light teal to dark teal, with each cell labelled with its count](https://www.pythoncharts.com/plotly/plotly-colors/images/plotly-colors-custom-scale.png)

The first position must be 0 and the last 1. To go the other way and pick a few discrete colors from a continuous scale, use `px.colors.sample_colorscale("Viridis", 5)`, which returns five `rgb()` strings you can pass to `color_discrete_sequence=`. The [heatmap post](https://www.pythoncharts.com/plotly/plotly-heatmaps/) has more on `px.imshow`.

## Set colors with `graph_objects`

With `go` traces, color is a trace property. A single value colors the whole trace, and a list colors each point or bar:

```python
import plotly.graph_objects as go

colors = ["#ea580c" if value == sales["revenue"].max() else "#94a3b8"
          for value in sales["revenue"]]

fig = go.Figure(go.Bar(x=sales["region"], y=sales["revenue"], marker_color=colors,
                       marker_line=dict(color="#334155", width=1)))
fig.add_trace(go.Scatter(x=sales["region"], y=[sales["revenue"].mean()] * 5,
                         mode="lines", line=dict(color="#0f172a", dash="dash"),
                         name="Average"))
fig.update_layout(title="Highest region in orange", showlegend=False,
                  width=700, height=380)
fig.show()
```

![Plotly graph_objects bar chart with gray bars outlined in dark slate, the highest region East in orange, and a dashed black line at the average revenue of 3.9 million dollars](https://www.pythoncharts.com/plotly/plotly-colors/images/plotly-colors-graph-objects.png)

| Trace | Property |
|---|---|
| `go.Bar`, `go.Scatter` markers | `marker_color=` (one color, a list, or numbers with `marker_colorscale=`) |
| `go.Scatter` lines | `line_color=` |
| Bar and marker outlines | `marker_line_color=`, `marker_line_width=` |
| Filled areas | `fillcolor=` |
| `go.Heatmap`, `go.Surface` | `colorscale=`, plus `zmid=` to center it |

To change a Plotly Express figure after it's made, the same properties work through `fig.update_traces(marker_color="#0f766e")`, or with `selector=dict(name="Web")` to change one trace.

## Set a default palette for every chart

To use one palette in every figure of a notebook or app, set it once instead of passing it to each call:

```python
import plotly.io as pio

px.defaults.color_discrete_sequence = px.colors.qualitative.Safe  # Plotly Express only
px.defaults.color_continuous_scale = "Viridis"

pio.templates["brand"] = go.layout.Template(pio.templates["plotly_white"])
pio.templates["brand"].layout.colorway = ["#1d4ed8", "#16a34a", "#ea580c", "#9333ea"]
pio.templates.default = "brand"  # also applies to graph_objects figures

pio.templates.default = "plotly"  # back to the default
px.defaults.color_discrete_sequence = None
px.defaults.color_continuous_scale = None
```

`px.defaults` only changes Plotly Express. A template's `colorway` is the list every trace draws from when it has no color of its own, so it covers `go` figures too. [Plotly templates and themes](https://www.pythoncharts.com/plotly/plotly-templates-themes/) covers building a full theme.

## Common problems

| Problem | Cause | Fix |
|---|---|---|
| `color_discrete_sequence` is ignored | The `color` column is numeric, so Plotly uses a continuous scale | `color=df["col"].astype(str)`, or use `color_continuous_scale=` |
| Colors swap between charts | A sequence follows the order of the data | Use `color_discrete_map=` |
| A category in `color_discrete_map` is missing | The key doesn't match the column value exactly | Check spelling and case; unmatched categories fall back to the sequence |
| `Invalid value ... for the 'colorscale' property` | Misspelled scale name | Check `px.colors.named_colorscales()` |
| The diverging scale isn't centered | The data isn't symmetric around the center | `color_continuous_midpoint=0` (Plotly Express) or `zmid=0` (`go.Heatmap`) |
| Transparent colors | Plotly takes `rgba()` strings | `"rgba(29, 78, 216, 0.4)"`, or `opacity=0.4` for the whole trace |

## Similar Topics

- [Plotly Templates and Themes: Built-In and Custom](https://www.pythoncharts.com/plotly/plotly-templates-themes/)
- [Plotly Scatter Plot: px.scatter() With Color, Size, Hover, and Trendlines](https://www.pythoncharts.com/plotly/plotly-scatter-plot/)
- [Setting Custom Color Palettes in Seaborn](https://www.pythoncharts.com/seaborn/seaborn-custom-color-palettes/)
