【问题标题】:How to plot plotly gauge charts next to each other with python?python - 如何使用python绘制相互相邻的图表?
【发布时间】:2020-10-21 11:43:24
【问题描述】:

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

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如何并排显示?

【问题讨论】:

  • 您收到了两个类似但各自以自己的方式有价值和有效的建议。如果您发现它们对自己有用,请考虑给予支持。也许还可以考虑将最适合您需求的答案标记为已接受的答案。

标签: python plotly gauge


【解决方案1】:

您在使用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()

【讨论】:

  • 非常感谢 - 谢谢。是否可以做一个并排的仪表图,而不是 trace1 和 trace2 是完整的 go.Indicator() 代码,而是从创建仪表指标图表并返回 fig.show 的函数调用 trace1 和 trace2 () 为使用该函数创建的每个仪表指标?换句话说,我可以调用一个使用 go.Indicator() 的函数来创建仪表图,而不是像上面那样在 trace1 和 trace2 处创建仪表图吗?我试过了,但无法让它工作。也许函数返回了错误的东西(fig.show())?谢谢!
  • @GbG 感谢您的反馈。我不确定你所问的是否真的可能。如果是这样,我相信如果你有时间把它写成一个新问题,你会得到你正在寻求的帮助。如果您这样做,我会确保仔细查看它。
  • 谢谢@vestland。非常感谢。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 2018-02-16
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2021-03-04
    • 1970-01-01
    相关资源
    最近更新 更多