Open In App

Python Plotly – How to set up a color palette?

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Plotly a color palette is a set of colors that you can use to make your plots look more attractive. You can set up a color palette to control how the colors in your graph. In this article, we will discuss how to set up a color palette in plotly.

Method 1: Setting up the color palette for continuous data

px. scatter() method is used to plot a scatterplot of continuous data. We explicitly make a color palette by making a list of the colors. The list is passed to the colour_continuous_scale parameter of the px.scatter() method as we’re working on continuous data.

Click here to download the CSV file.

Python
import pandas as pd
import plotly.express as px

df = pd.read_csv('weather.csv', encoding='UTF-8')

fig = px.scatter(df, x="Temperature", y='Humidity', color='Light',
                 title="setting up colour palette", 
                 color_continuous_scale=["orange", "red", 
                                         "green", "blue",
                                         "purple"])

fig.show()

Output:

This scatter plot visualizes the relationship between Temperature and Humidity with point colors representing Light intensity using a custom color palette. Brighter colors (yellow to red) indicate lower light levels, while darker (green to purple) shows higher intensity

Method 2: Setting up the color palette for discrete data

px. bar() method is used to plot a barplot of the data we provide. The list of colours is passed to the color_discrete_sequence parameter of the px.bar() method. as we’re working on discrete data. Click here to download the used CSV file.

Python
import pandas as pd
import plotly.express as px

df = pd.read_csv('country_density.csv', encoding='UTF-8')

df = df.iloc[:5, :]

fig = px.bar(df, x="Country", y="Density_(P/Km²)", color="Country", 
             orientation="v", hover_name="Country",
             color_discrete_sequence=[
                 "orange", "red", "green", "blue", "purple"],
             title="Explicit color sequence"
             )

fig.show()

Output:

Note: if our color palette contains fewer colors than our groups or labels then colors repeat themselves. Take a look at this example to visualize.

Python
import pandas as pd
import plotly.express as px

df = pd.read_csv('country_density.csv', encoding='UTF-8')

df = df.iloc[:5, :]

fig = px.bar(df, x="Country", y="Density_(P/Km²)", color="Country", 
             orientation="v", hover_name="Country",
             color_discrete_sequence=["orange", "red"],
             title="Explicit color sequence"
             )

fig.show()

Output:

This bar chart shows population density (P/km²) for five countries using an explicitly defined color sequence. India has the highest density while the United States has the lowest.

Method 3: Setting up color palettes from already existing color palettes

In Plotly you can easily use existing color palettes to enhance the appearance of your plots. Plotly offers a variety of predefined color scales and discrete color sequences that you can choose from. We can see Plotly’s built-in qualitative color palettes using px.colors.qualitative.swatches().

Python
import pandas as pd
import plotly.express as px

fig = px.colors.qualitative.swatches()
fig.show()

Output:

This image shows all Plotly’s built-in qualitative color palettes. Each row represents a named palette with its unique set of colors for discrete data visualization.

In below example we index the colors from already existing color palettes and form a new color palette instead of giving names in string format this can also be done. This method can be used if we want to pick colors from already existing palettes or if we don’t know the name of the color. The format of indexing is:

px.colors.qualitative.colour_sequence_name[index]

 example: px.colors.qualitative.Alphabet[11]

Python
import pandas as pd
import plotly.express as px

df = pd.read_csv('country_density.csv', encoding='UTF-8')

df = df.iloc[:5, :]

fig = px.bar(df, x="Country", y="Density_(P/Km²)", color="Country",
             orientation="v",
             hover_name="Country", color_discrete_sequence=[
                 px.colors.qualitative.Alphabet[6],
                 px.colors.qualitative.Alphabet[11],
               px.colors.qualitative.Plotly[2],
                 px.colors.qualitative.Plotly[7],
               px.colors.qualitative.G10[5]],
             title="Explicit color sequence"
             )

fig.show()

Output:

The above bar chart of population density shows five countries. Each country is color-coded using a custom scheme from the Alphabet plotly built-in palettes.



Next Article

Similar Reads