# Gauge Chart with Python

**URL:** <https://community.plotly.com/t/gauge-chart-with-python/57279>\
**Category:** 📊 Plotly Python\
**Created:** [October 5, 2021, 7:16pm UTC](https://community.plotly.com/t/gauge-chart-with-python/57279 "2021-10-05T19:16:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitS](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@pitS](https://community.plotly.com/u/pitS)\
**Post date:** [October 5, 2021, 7:16pm UTC](https://community.plotly.com/t/gauge-chart-with-python/57279/1 "2021-10-05T19:16:20Z")

</div>

Hi, i would like to create a following gauge chart, but i am not able to figure it out.  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/6/8/683f10db8474ed69ee00adac2fc52949e49d8b08.png)

---

<div class="post-metadata">

**Author:** ![RenaudLN](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/renaudln/32/9041_2.png) [@RenaudLN](https://community.plotly.com/u/RenaudLN)\
**Post date:** [October 5, 2021, 11:43pm UTC](https://community.plotly.com/t/gauge-chart-with-python/57279/2 "2021-10-05T23:43:32Z")

</div>

Hey, if this is to use within Dash you could use the dash acquisition component:  
[https://dash.plotly.com/dash-daq/gauge](https://dash.plotly.com/dash-daq/gauge)

If it’s outside of Dash, you will need to do a custom one combining a holed pie chart and shapes for the needle.

---

<div class="post-metadata">

**Author:** ![pitS](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@pitS](https://community.plotly.com/u/pitS)\
**Post date:** [October 6, 2021, 5:33am UTC](https://community.plotly.com/t/gauge-chart-with-python/57279/3 "2021-10-06T05:33:53Z")

</div>

I would like to have a solution outside of Dash. I was not able to find neither a semi donut chart neither a needle chart. Has anybody made a similar chart already?

---

<div class="post-metadata">

**Author:** ![RenaudLN](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/renaudln/32/9041_2.png) [@RenaudLN](https://community.plotly.com/u/RenaudLN)\
**Post date:** [October 6, 2021, 8:17am UTC](https://community.plotly.com/t/gauge-chart-with-python/57279/4 "2021-10-06T08:17:30Z")

</div>

You could do something custom, see example below.  
The main drawbacks are:

- No interactivity on hover (could be solved with overlaying a transparent scatterplot or using a line plot for the hand)
- The pie is centered in the figure so it leaves some blank space at the bottom

```python
import plotly.graph_objects as go
import numpy as np

plot_bgcolor = "#def"
quadrant_colors = [plot_bgcolor, "#f25829", "#f2a529", "#eff229", "#85e043", "#2bad4e"] 
quadrant_text = ["", "<b>Very high</b>", "<b>High</b>", "<b>Medium</b>", "<b>Low</b>", "<b>Very low</b>"]
n_quadrants = len(quadrant_colors) - 1

current_value = 19
min_value = 0
max_value = 50
hand_length = np.sqrt(2) / 4
hand_angle = np.pi * (1 - (max(min_value, min(max_value, current_value)) - min_value) / (max_value - min_value))

fig = go.Figure(
    data=[
        go.Pie(
            values=[0.5] + (np.ones(n_quadrants) / 2 / n_quadrants).tolist(),
            rotation=90,
            hole=0.5,
            marker_colors=quadrant_colors,
            text=quadrant_text,
            textinfo="text",
            hoverinfo="skip",
        ),
    ],
    layout=go.Layout(
        showlegend=False,
        margin=dict(b=0,t=10,l=10,r=10),
        width=450,
        height=450,
        paper_bgcolor=plot_bgcolor,
        annotations=[
            go.layout.Annotation(
                text=f"<b>IOT sensot value:</b><br>{current_value} units",
                x=0.5, xanchor="center", xref="paper",
                y=0.25, yanchor="bottom", yref="paper",
                showarrow=False,
            )
        ],
        shapes=[
            go.layout.Shape(
                type="circle",
                x0=0.48, x1=0.52,
                y0=0.48, y1=0.52,
                fillcolor="#333",
                line_color="#333",
            ),
            go.layout.Shape(
                type="line",
                x0=0.5, x1=0.5 + hand_length * np.cos(hand_angle),
                y0=0.5, y1=0.5 + hand_length * np.sin(hand_angle),
                line=dict(color="#333", width=4)
            )
        ]
    )
)
fig.show()

```

![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/e/b/eb37a671d7ecf1578fceb00257a8814be1ebba1a.png)

---

<div class="post-metadata">

**Author:** ![dmbn](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@dmbn](https://community.plotly.com/u/dmbn)\
**Post date:** [February 10, 2022, 7:32am UTC](https://community.plotly.com/t/gauge-chart-with-python/57279/5 "2022-02-10T07:32:03Z")

</div>

That solution looks great, but is there a possibility to crop that bottom part with invisible sector just below the anntotation text? Smaller height doesn’t really work - it squeezes the chart and the circle.

---

<div class="post-metadata">

**Author:** ![RenaudLN](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/renaudln/32/9041_2.png) [@RenaudLN](https://community.plotly.com/u/RenaudLN)\
**Post date:** [February 10, 2022, 12:01pm UTC](https://community.plotly.com/t/gauge-chart-with-python/57279/6 "2022-02-10T12:01:21Z")

</div>

Like I said in the message: no, you can’t have a half pie chart so no removing that bottom part. I’m sure you could make it work with smaller sizes though.

---

<div class="post-metadata">

**Author:** ![yogi\_dhiman](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yogi\_dhiman](https://community.plotly.com/u/yogi_dhiman)\
**Post date:** [June 24, 2024, 6:26pm UTC](https://community.plotly.com/t/gauge-chart-with-python/57279/7 "2024-06-24T18:26:15Z")

</div>

Can we add values at circumference of odometer . So that we know needle is near what value
