Member-only story

Gauge charts using Python

Python Coding
2 min readOct 9, 2024

--

import plotly.graph_objects as go

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

Let’s break down the code step by step:

1. import plotly.graph_objects as go

  • This imports the graph_objects module from the Plotly library. It is a module that contains various graphing functions for creating different types of charts.
  • The alias go is used for convenience so that instead of typing plotly.graph_objects, you can simply use go.

2. fig = go.Figure(go.Indicator(...))

  • This line creates a Figure object named fig. A Figure is a container that holds the chart or plot.
  • The go.Indicator function is used to create an indicator chart. In this case, we are creating a gauge chart that shows a number and a dial.

3. mode="gauge+number"

  • The mode specifies the visual components of the indicator. Here, "gauge+number" means the chart will display both a gauge (the circular dial) and the numeric value.

4. value=45

  • This is the current value to be displayed by the gauge. The needle will point to 45 on the dial.

5. title={'text': "Speed"}

  • This sets the title of the gauge chart to "Speed". You can customize the title text to whatever label you need.

6. gauge={'axis': {'range': [0, 100]}, ...}

  • This is where the gauge-specific configurations are defined:
  • axis: Specifies the range of the gauge's dial, from 0 to 100.
  • bar: Specifies the color of the gauge's needle bar (the needle itself), set to "darkblue."
  • steps: Adds colored regions to the gauge for visual clarity:
  • The first step ([0, 50]) is colored "lightgray."
  • The second step ([50, 100]) is colored "gray."
  • threshold: This defines a threshold marker on the gauge:
  • A red line will be drawn at the value of 80.
  • line defines the color ("red") and width of this threshold line.
  • thickness controls the thickness of the line.

7. fig.show()

  • This command renders the gauge chart and displays it. In a Jupyter Notebook, the chart will be displayed inline.

In summary, this code creates a gauge chart with a title of “Speed,” a needle pointing to 45, and a range from 0 to 100, with custom color steps and a threshold indicator at 80.

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

No responses yet