Continuous Color Scales and Color Bars in Python

How to set, create and control continuous color scales and color bars in scatter, bar, map and heatmap figures.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

Continuous vs Discrete Color

In the same way as the X or Y position of a mark in cartesian coordinates can be used to represent continuous values (i.e. amounts or moments in time) or categories (i.e. labels), color can be used to represent continuous or categorical data. This page is about using color to represent continuous data, but Plotly can also represent categorical values with color.

Continuous Color Concepts

This document explains the following four continuous-color-related concepts:

  • color scales represent a mapping between the range 0 to 1 and some color domain within which colors are to be interpolated (unlike discrete color sequences which are never interpolated). Color scale defaults depend on the layout.colorscales attributes of the active template, and can be explicitly specified using the color_continuous_scale argument for many Plotly Express functions or the colorscale argument in various graph_objects such as layout.coloraxis or marker.colorscale in go.Scatter traces or colorscale in go.Heatmap traces. For example [(0,"blue"), (1,"red")] is a simple color scale that interpolated between blue and red via purple, which can also be implicitly represented as ["blue", "red"] and happens to be one of the built-in color scales and therefore referred to as "bluered" or plotly.colors.sequential.Bluered.
  • color ranges represent the minimum to maximum range of data to be mapped onto the 0 to 1 input range of the color scale. Color ranges default to the range of the input data and can be explicitly specified using either the range_color or color_continuous_midpoint arguments for many Plotly Express functions, or cmin/cmid/cmax or zmin/zmid/zmax for various graph_objects such as layout.coloraxis.cmin or marker.cmin in go.Scatter traces or cmin in go.Heatmap traces. For example, if a color range of [100, 200] is used with the color scale above, then any mark with a color value of 100 or less will be blue, and 200 or more will be red. Marks with values in between will be various shades of purple.
  • color bars are legend-like visible representations of the color range and color scale with optional tick labels and tick marks. Color bars can be configured with attributes inside layout.coloraxis.colorbar or in places like marker.colorbar in go.Scatter traces or colorbar in go.Heatmap traces.
  • color axes connect color scales, color ranges and color bars to a trace's data. By default, any colorable attribute in a trace is attached to its own local color axis, but color axes may also be shared across attributes and traces by setting e.g. marker.coloraxis in go.Scatter traces or coloraxis in go.Heatmap traces. Local color axis attributes are configured within traces e.g. marker.showscale whereas shared color axis attributes are configured within the Layout e.g. layout.coloraxis.showscale.

Continuous Color with Plotly Express

Most Plotly Express functions accept a color argument which automatically assigns data values to continuous color if the data is numeric. If the data contains strings, the color will automatically be considered discrete (also known as categorical or qualitative). This means that numeric strings must be parsed to be used for continuous color, and conversely, numbers used as category codes must be converted to strings.

For example, in the tips dataset, the size column contains numbers:

In [1]:
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="size",
                 title="Numeric 'size' values mean continuous color")

fig.show()

Converting this column to strings is very straightforward:

In [2]:
import plotly.express as px
df = px.data.tips()
df["size"] = df["size"].astype(str)
fig = px.scatter(df, x="total_bill", y="tip", color="size",
                 title="String 'size' values mean discrete colors")

fig.show()

If you have stringified numbers you can convert back just as easily:

In [3]:
import plotly.express as px
df = px.data.tips()
df["size"] = df["size"].astype(str)
df["size"] = df["size"].astype(float)
fig = px.scatter(df, x="total_bill", y="tip", color="size",
                 title="Numeric 'size' values mean continuous color")

fig.show()

Colorscales in Dash

Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run pip install dash, click "Download" to get the code and run python app.py.

Get started with the official Dash docs and learn how to effortlessly style & publish apps like this with Dash Enterprise or Plotly Cloud.

Out[4]:

Sign up for Dash Club → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now.

Color Scales in Plotly Express

By default, Plotly Express will use the color scale from the active template's layout.colorscales.sequential attribute, and the default active template is plotly which uses the Plasma color scale. You can choose any of the built-in color scales, however, or define your own.

Here is an example that creates a scatter plot using Plotly Express, with points colored using the Viridis color scale.

In [5]:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="sepal_length", color_continuous_scale=px.colors.sequential.Viridis)

fig.show()

It is also possible to specify color scales by name. Here is an example that specifies the Inferno color scale by name, as a string

In [6]:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="sepal_length", color_continuous_scale='Inferno')

fig.show()

Reversing a built-in color scale

You can reverse a built-in color scale by appending _r to its name, for color scales given either as a string or a plotly object.

In [7]:
import plotly.express as px
df = px.data.gapminder().query("year == 2007").sort_values(by="lifeExp")
fig = px.bar(df, y="continent", x="pop", color="lifeExp", orientation="h",
             color_continuous_scale='Bluered_r', hover_name="country")

fig.show()
In [8]:
import plotly.express as px
data = [[1, .3, .5, .9],
        [.3, .1, .4, 1],
        [.2, .8, .9, .3]]
fig = px.imshow(data, color_continuous_scale=px.colors.sequential.Cividis_r)
fig.show()

Explicitly Constructing a Color scale

The Plotly Express color_continuous_scale argument accepts explicitly-constructed color scales as well.

The Graph Objects colorscale parameter also accepts an explicitly-constructed color scale, as shown in the example below.

In [9]:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="sepal_length",
                 color_continuous_scale=["red", "green", "blue"])

fig.show()