Site icon FSIBLOG

How to Create Gauge Charts Using Python

How to Create Gauge Charts Using Python

How to Create Gauge Charts Using Python

Have you ever wanted to display data in a speedometer style chart for things like CPU usage, vehicle speed, or performance scores. I have and when I discovered gauge charts, I realized they were the perfect tool for the job.

I’ll walk you through how to create interactive gauge charts using Python and Plotly. I’ll start with a simple example, explain each part of the code, and then show you how to add more features so you can experiment on your own.

Here the Basic Code to Create a Gauge Chart

Let me start with a simple gauge chart that shows a speed value.

import plotly.graph_objects as go

fig = go.Figure(go.Indicator(
mode="gauge+number", # Shows both the gauge and the numeric value
value=65, # The current value displayed on the gauge
title={'text': "Speed"}, # Title displayed above the chart
gauge={
'axis': {'range': [0, 100]}, # The range of values (0 to 100)
'bar': {'color': "darkblue"}, # The pointer or filled bar color
'steps': [
{'range': [0, 50], 'color': "lightgray"}, # Color of range 0–50
{'range': [50, 100], 'color': "gray"} # Color of range 50–100
],
'threshold': {
'line': {'color': "red", 'width': 4}, # Red warning line
'thickness': 0.75,
'value': 80 # Warning zone starts at 80
}
}
))

fig.show()

What Is This Code Doing

Want to Practice More Try These Enhancement

Make the Value Dynamic Using Input

Here’s a version where I can enter the speed manually:

import plotly.graph_objects as go

# Ask the user to input a value
current_speed = float(input("Enter the current speed (0–100): "))

fig = go.Figure(go.Indicator(
mode="gauge+number",
value=current_speed,
title={'text': "Vehicle Speed"},
gauge={
'axis': {'range': [0, 100]},
'bar': {'color': "blue"},
'steps': [
{'range': [0, 50], 'color': "lightgreen"},
{'range': [50, 80], 'color': "orange"},
{'range': [80, 100], 'color': "gray"}
],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': 90
}
}
))

fig.show()

Now, when I run the script, it asks me to type in a number and instantly visualizes it!

Customize the Title and Range

Let’s go one step further: what if I want to track something other than speed like temperature, or system load

This version lets me change the title and axis range dynamically:

import plotly.graph_objects as go

custom_title = input("Enter the gauge title: ")
min_range = int(input("Enter the minimum range: "))
max_range = int(input("Enter the maximum range: "))
value = float(input(f"Enter the current value ({min_range}–{max_range}): "))

fig = go.Figure(go.Indicator(
mode="gauge+number",
value=value,
title={'text': custom_title},
gauge={
'axis': {'range': [min_range, max_range]},
'bar': {'color': "darkblue"},
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': max_range * 0.8
}
}
))

fig.show()

Now I can create a gauge for anything from battery levels to temperature zones.

Bonus Challenge Ideas

If you’re like me and love playing with ideas, here are some challenges for you:

Final Thought

Gauge charts are a fantastic way to turn numbers into visuals. Whether you’re showing speed, usage, or scores, they make your data pop in a dashboard. I personally love how easy and beautiful Plotly makes it to build these charts with just a few lines of code, I can create something that’s not only functional but also visually appealing.

Exit mobile version