Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot plotly gauge charts next to each other with python?

I have two gauge plots in my script and want to visualize them, side by side.

import plotly.graph_objects as go

fig1 = go.Figure(go.Indicator(mode="gauge+number",    value=400,    domain={'x': [0, 1], 'y': [0, 1]},    title={'text': "Speed 1"}))

fig2 = go.Figure(go.Indicator(mode="gauge+number",    value=250,    domain={'x': [0, 1], 'y': [0, 1]},    title={'text': "Speed 2"}))

How can show fig1 and fig2 side by side?

like image 906
cheesus Avatar asked Nov 15 '25 17:11

cheesus


2 Answers

You can show both figures side by side using subplots.

import plotly.graph_objs as go
from plotly.subplots import make_subplots

trace1 = go.Indicator(mode="gauge+number",    value=400,    domain={'row' : 1, 'column' : 1}, title={'text': "Speed 1"})
trace2 = go.Indicator(mode="gauge+number",    value=250,    domain={'row' : 1, 'column' : 2}, title={'text': "Speed 2"})

fig = make_subplots(
    rows=1,
    cols=2,
    specs=[[{'type' : 'indicator'}, {'type' : 'indicator'}]],
    )

fig.append_trace(trace1, row=1, col=1)
fig.append_trace(trace2, row=1, col=2)

fig.show()

enter image description here

like image 120
jgrt Avatar answered Nov 17 '25 09:11

jgrt


You're on the right track using the domain attribute, but your exact specifications are off. With the rest of the setup in the complete code below, the following specifications produces the associated plot:

Domain specs

 domain={'x': [0.0, 0.4], 'y': [0.0, 1]}

 domain={'x': [0.6, 1.0], 'y': [0., 1.00]}

Plot

enter image description here

Complete code

import plotly
import plotly.offline as py
import plotly.graph_objs as go
import plotly.express as px

# traces with separate domains to form a subplot
trace1 = go.Indicator(mode="gauge+number",    value=400,    domain={'x': [0.0, 0.4], 'y': [0.0, 1]},    title={'text': "Speed 1"})

trace2 = go.Indicator(mode="gauge+number",    value=250,    domain={'x': [0.6, 1.0], 'y': [0., 1.00]},    title={'text': "Speed 2"})

# layout and figure production
layout = go.Layout(height = 600,
                   width = 600,
                   autosize = False,
                   title = 'Side by side gauge diagrams')
fig = go.Figure(data = [trace1, trace2], layout = layout)
fig.show()
like image 33
vestland Avatar answered Nov 17 '25 10:11

vestland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!