Creating and Customizing Indicator Charts with Plotly

Arun
4 min readMay 22, 2023

--

Welcome to this beginner-friendly tutorial where we’re going to unravel the mystery of creating indicator charts using a popular Python library called Plotly. If you’re not familiar with Plotly or the concept of data visualization, fret not! We’re going to break it down into bite-sized chunks, making it palatable for anyone and everyone.

Introduction to Plotly

Plotly is a powerful data visualization tool that lets us create a wide array of interactive plots and charts using Python. Among the myriad types of plots that Plotly has to offer, one particular type that stands out for its simplicity and clarity is the ‘Indicator Chart’. These charts are superbly suitable for dashboards where you want to underscore key metrics or trace changes over a period of time.

Setting the Stage

Before we delve into the details, we need to import the Plotly library. This is the typical first step in any Python program that involves an external library:

import plotly.graph_objects as go

Creating Indicator Charts

Let’s consider a scenario where we have two values — 120 and 150. We’re interested in creating indicators for these values using a reference value of 100. The reference value is a benchmark or a standard that we compare our actual values against.

Output of Figure

Here’s how we can bring our indicators to life:

# Create the layout of the plot
layout = go.Layout(
grid = {'rows': 1, 'columns': 2, 'pattern': 'independent'},
width = 600, # Width in pixels
height = 300 # Height in pixels
)
fig = go.Figure(layout=layout)# First value
fig.add_trace(go.Indicator(
mode = "number+delta+gauge",
value = 120,
delta = {'reference': 100},
gauge = {
'axis': {'visible': True, 'range': [None, 150]},
'steps': [
{'range': [0, 100], 'color': "lightgray"}
],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': 100
}
},
title = {"text": "Value 1"},
domain = {'x': [0, 0.5], 'y': [0, 1]}
))
# Second value
fig.add_trace(go.Indicator(
mode = "number+delta+gauge",
value = 150,
delta = {'reference': 100},
gauge = {
'axis': {'visible': True, 'range': [None, 200]},
'steps': [
{'range': [0, 100], 'color': "lightgray"}
],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': 100
}
},
title = {"text": "Value 2"},
domain = {'x': [0.5, 1], 'y': [0, 1]}
))
fig.show()

In the code we’ve discussed, we’re using the go.Indicator function to create our indicators. It might seem a bit complex at first, but once we understand the parameters it uses, it all falls into place. Here's a rundown of the parameters we've used:

  • mode: This parameter sets the mode of the indicator. By setting it to "number+delta+gauge", we're telling Plotly to display the actual value (number), the difference from the reference value (delta), and a gauge to visualize the value in the context of its range (gauge).
  • value: This is the actual value that the indicator will display.
  • delta: This parameter is used to showcase the difference between the actual value and the reference value. This is particularly useful when you want to highlight change or growth.
  • gauge: This is a visual representation that provides context for our value. It's akin to a speedometer that shows where our value lies within a specific range. Within the gauge parameter, we can further customize the axis (the range of our gauge), steps (segments of the gauge), and threshold (a marked value on the gauge).
  • title: This is simply the title of the indicator.
  • domain: This parameter determines the area of the plot that each indicator occupies, which helps us position the indicators side by side.

By understanding and tweaking these parameters, you can customize the indicator charts to suit your data and your audience’s needs. You can play around with colors, adjust the range, reposition the indicators, and much more.

However, while aesthetic customization is a wonderful aspect of data visualization, it’s important to remember that the primary goal of any data visualization is to clearly and accurately represent the data. So, always prioritize clarity over fanciness.

And there you have it! We’ve created a simple yet powerful data visualization using Plotly. This is just the tip of the iceberg — Plotly offers many more features and types of plots that you can explore. The world of data visualization is vast and endlessly fascinating. So, keep exploring, keep learning, and most importantly, have fun!

--

--