如何使用 python 绘制彼此相邻的 plotly gauge 图表?

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

我的脚本中有两个仪表图,我想将它们并排可视化。

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"}))

如何并排显示 fig1fig2

您可以使用子图并排显示两个图。

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()

您使用 domain 属性的方法是正确的,但您的确切规格不正确。使用下面完整代码中的其余设置,以下规范生成相关的绘图:

域规范

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

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

情节

完整代码

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()