Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Editing subplot layouts (Adding shapes, drop-down menus)

Is there a way to edit subplot layouts beyond modifying the axes? I know the layout for the entire figure can be accessed as a dictionary (e.g. fig[‘layout’]) but can a full layout dictionary be accessed for the subplots?

I would like to be able to use something like fig[‘layout’][‘subplot1layout’].update(shapes=[{‘type’: ‘rect’, ‘x0’: 0, ‘x1’: 1, ‘y0’: 0, ‘y1’: 1, ‘line’: {‘color’: ‘rgba(0, 0, 0, 1)’}}]

Hi there. Take a look at this tutorial. It looks like you want to access the actual data in each subplot. That information is found in data in the fig object. So something like fig['data'][0] for one of the subplots.

@Adam if willing to define a different shape for each subplot, would those need to be defined with the data or with with the layout?


I apologize for the late reply.

You would need to do it with the layout. Look at this example. The parameters xaxis1, xaxis2, etc are used to denote the domains of each of these axes. To change the subplot layout, change these values.

For those who think the tutorials referenced above do not demonstrate how to draw shapes on subplots here is an example that worked for me:

import plotly.offline as ofl
import plotly.plotly as py
import as tls
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[10, 20, 30],
    y=[40, 50, 60]
trace2 = go.Scatter(
    x=[20, 30, 40],
    y=[50, 60, 70]
trace3 = go.Scatter(
    x=[30, 40, 50],
    y=[60, 70, 80]
trace4 = go.Scatter(
    x=[40, 50, 60],
    y=[70, 80, 90]
shapes = [
    {'type': 'line', 'x0': 25, 'y0': 100, 'x1': 35, 'y1': 100, 'xref': 'x1', 'yref': 'y1'},
    {'type': 'line', 'x0': 25, 'y0': 200, 'x1': 35, 'y1': 200, 'xref': 'x2', 'yref': 'y2'},
    {'type': 'line', 'x0': 25, 'y0': 300, 'x1': 35, 'y1': 300, 'xref': 'x3', 'yref': 'y3'},
    {'type': 'line', 'x0': 25, 'y0': 400, 'x1': 35, 'y1': 400, 'xref': 'x4', 'yref': 'y4'}

fig = tls.make_subplots(rows=2, cols=2)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 2, 1)
fig.append_trace(trace4, 2, 2)


ofl.iplot(fig, filename='multiple-subplots')

That was super useful thanks for clarifying @plexoos.

This works excellent. I tried using add_hlines first but col and row properties didnt work for me.