【问题标题】:x axes titles on subplots Plotly Python子图上的 x 轴标题 Plotly Python
【发布时间】:2020-05-14 20:05:49
【问题描述】:

我正在尝试将x-axesy-axes 上的相同标题添加到显示为子图的四个甘特图中。我在网上找到了figs.update_layout() 但是,这只是在第一个图表上显示标题,而不是全部。我用 R 找到了一些答案,但是对 python 的任何帮助都会很棒

figs = make_subplots(
rows=2, cols=2,
shared_xaxes=False,
subplot_titles =('Plot 1', 'PLot 2', 'PLot 3', 'Plot 4')
)
figs.update_layout(
    title="Plot Title",
    xaxis_title="Miliseconds",
    yaxis_title="Services",
)

for trace in fig_operable.data:
    figs.add_trace(trace, row=1, col=1)
for trace in fig_dynamic.data:
    figs.add_trace(trace, row=2, col=1)
for trace in fig_early.data:
    figs.add_trace(trace, row=1, col=2)
for trace in fig_hmi.data:
    figs.add_trace(trace, row=2, col=2)

figs.update_layout(showlegend=False, title_text="Title of charts")
figs.show()

【问题讨论】:

    标签: python plotly axes gantt-chart


    【解决方案1】:

    IIUC 你正在寻找类似的东西

    import numpy as np
    import plotly.graph_objs as go
    from plotly.subplots import make_subplots
    
    subplot_titles = ['Plot 1', 'Plot 2', 'Plot 3', 'Plot 4']
    xaxis_title="Miliseconds"
    yaxis_title="Services"
    rows = 2
    cols = 2
    height = 300 * rows
    
    trace = go.Scatter(x=np.linspace(1,100,100),
                       y=np.linspace(1,100,100))
    
    
    fig = make_subplots(rows=rows, cols=cols,
                        shared_xaxes=False,
                        subplot_titles=subplot_titles
                        )
    
    for i, col in enumerate(subplot_titles):
        r = int(np.ceil(((i+1)/cols)))
        c = i%2+1
        fig.add_trace(trace, row=r, col=c)
        fig.update_xaxes(title_text=xaxis_title, row=r, col=c)
        fig.update_yaxes(title_text=yaxis_title, row=r, col=c)
    
    fig.update_layout(showlegend=False,
                      title_text="Title of charts",
                      title_x=0.5,
                      height=height)
    fig.show()    
    

    如果您还有其他疑问,可以查看文档here

    【讨论】:

    • 太棒了,谢谢。我正在使用我之前创建的甘特图。最好将它们插入到哪里进行跟踪?
    • 如果你有 4 个甘特图,你可以将它们放在一个名为 traces 的列表中,并在 add_trace 中使用 traces[i] 而不是 trace
    猜你喜欢
    • 1970-01-01
    • 2021-01-03
    • 2020-10-06
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2017-12-25
    • 2017-05-10
    相关资源
    最近更新 更多