简体   繁体   中英

x axes titles on subplots Plotly Python

I am trying to add the same titles on the x-axes and y-axes to four gantt charts that are displayed as subplots. I found online figs.update_layout() however, this only presents the title on the first chart, not all. I have found a few answers with R however any help with python would be great

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

IIUC you are looking for something like

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

在此处输入图片说明

If you need any other doubts you can have a look at the documentation here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM