Python Friday #193: Choosing Colours for Plotly

As we have seen in the last post, Plotly does not always use colours with a high enough contrast. In this post we explore the different options we have to choose better colours.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

CSS colours

Plotly as a JavaScript library uses CSS colours throughout its styling options. We can find the whole list of pre-defined CSS colours in the Mozilla Developer documentation. We not only get the names of the different colours, but also an example on how this colour looks.

 

Using custom colours

As a reminder, the default colours for the pie chart look not that great:

The default colours do not have a high enough contrast.

We can use our own set of CSS colours and use them as color_discrete_sequence parameter:

The pie chart now uses our CSS colours.

We can also use the HEX values for our colours to match the style of our corporate identity:

The colours are now much better visible.

 

Map specific values to a colour

If we have multiple plots and we want to show the same data with the same colour, we can use color_discrete_map and use a dictionary to map a category to a colour:

Our box plot now uses the pre-defined colours for the days of the week.

 

Discrete colours for categorical values

Plotly offers us a wide range of built-in colour sequences that we can use without first defining the specific colours. We can get the list of possible sequences with this code:

This creates us a sample of the colour sequences.

We can use the names of those sequences in our plots with the color_discrete_sequence parameter:

Our pie chart now uses the <strong>Vivid</strong> sequence.

We can reverse the colour selection by adding a _r at the end of the sequence name:

Our pie chart still uses the Vivid sequence, but this time it starts with the colours from the end.

 

Continuous colours for numerical values

For numerical values we need a more continuous sequence of colours. We can explore what Plotly can offer us with this command:

This gives us a sample for the continuous colour sequences that are built into Plotly.
Click on image to see the full sequence.

We can use these continuous colours with the color_continuous_scale parameter:

Our plot now uses the reversed Agsunset continuous colours for the tips.

Should we use these continuous colours for categorical values, Plotly will split the colour scale up for us:


We now get steps in our continuous colour scale
Click on image to see the full sequence.

We can use one of those continuous colours for our categorical values:

Our pie chart uses the reversed Agsunset as well.

 

Diverging colours

If we want a colour that shows the difference from a middle value, we can use any of the diverging sets:


Plotly offers us a nice set of diverging colours.
Click on image to see the full sequence.

We can apply those diverging colours in our plots:

Our scatterplot now uses the diverging colours of the Portland set.

 

Repeating colours

If we need a colour combination that is cyclical, we can use one of those colours with Plotly:

The selection of cyclical colours as wheels that comes with Plotly.

The selection of cyclical colours as bars that comes with Plotly.

 

Next

Getting the colours right is an important part of a nice visualisation. However, there is more to styling than the colours and we explore those options next week.

3 thoughts on “Python Friday #193: Choosing Colours for Plotly”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.