Adding traces to subplots

While I’ve been able to find plenty of documentation as to how to create subplots, as well as documentation as to how to create plots with multiple traces (doubt I need to site a source here, that’s fairly common), I’ve been alluded as to how to add a trace to a specific subplot. This is my code:

trace0=tracelist[0]
trace1=tracelist[1]
trace2=tracelist[2]
trace3=tracelist[3]

#guide for making subplots in python using plotly: https://plot.ly/python/subplots/

fig = tools.make_subplots(rows=2, cols=2, shared_yaxes=True)

fig = tools.make_subplots(rows=2, cols=2, subplot_titles=(titles[0],titles[1],titles[2],titles[3]),
                          horizontal_spacing=0.05,vertical_spacing=0.1, shared_yaxes=True)

fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 2)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 2, 2)

fig['layout']['xaxis1'].update(range=[-3, 7])
fig['layout']['xaxis3'].update(title='Pass',range=[-5,8],dtick=2)
fig['layout']['xaxis4'].update(title='Pass')

fig['layout']['yaxis1'].update(title='ytitle1')
fig['layout']['yaxis2'].update(title='ytitle2')

fig['layout'].update(height=800, width=1000, showlegend=False,
                     title='Title')
py.iplot(fig)

Whenever traces are added to a plot, it’s usually performed by creating a list of traces called ‘data,’ which is utilized when creating ‘fig.’ However, due to the nature of how subplots are created, I don’t see any way to apply that approach.

My goal is simply to add two horizontal lines to each of the four sub-plots; how exactly should I go about it? Is there some way I can utilize the ‘update’ method?

2 Likes

@Aeront I understood that you want to add more traces to the same subplot.
If this is the case, you should define a new trace, let us say this one:

one_more_trace=dict(type='scatter',
                                   x=[1,5],
                                   y=[1,3],
                                   mode='lines',
                                   line=dict(color='red'))

and append it to a subplot:

   fig.append_trace(one_more_trace, 1, 1)

In this way you can add/append new traces to each cell.

3 Likes

Hello

I’m having trouble adding a table, created with the go.Table method, to a subplot.

figure = tools.make_subplots(rows=3,
cols=2,
subplot_titles=(“a”, “b”, “c”, “d”,“e”),
specs=[[{}, {}],
[{}, {}],
[{“colspan”: 2}, None]
],
)

table = go.Table(…stuff…)
figure.append_trace(table, 3, 1)

this fails, and i kind of understand why (because table isn’t a trace). however, i’m really struggling to solve this (not least because my figure factory create_table method is always returning a heatmap and isn’t working (using latest plotly version 2.4.0)). i don’t really want to use the figure factory if possible.

ideally i would like to create my table using the go.Table method and have some way to then add the whole table to the subplot. Could someone help me and show me how?

Thanks in advance - i’m pretty new to python but think this Dash project is awesome. Great work!

Julian

@jcowkingmlp

A go.Table instance cannot be appended as trace via figure.append_trace(table, 3, 1), because this function assigns axes to table, and go.Table has no key ‘xaxis’, ‘yaxis’. The position of the table in the plot window is set via domain. See in these examples how you can insert a table as a subplot: https://plot.ly/~empet/14638.

2 Likes

Hi,

I am trying to add vertical lines on each subplots (all the vertical lines are static not hover dependent / click dependent, it should be shown all the time), so assume there are 3 points on the x-axis, so I have a dataframe where I have 3 rows of data, that is 3 time index of the dataframe, where I need to add a vertical dashed line on 2 subplots. is there a way we can do it? I tried few things but couldn’t get to that.

Was trying to use shapes for each subplots using .update method, but unable to.

fig[‘layout’][‘shapes’].update(type=‘line’,x=annotation.index,y=annotation.maxRate)

Not sure how to use crossline in here, bit new to python and using dash.

Thanks,
M

Did you end up finding a solution to this?
Having the same problem.

Hi @guy,

Here’s an example of add a line to two separate subplots

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

trace1 = go.Scatter(
    x=[1, 2, 3],
    y=[4, 5, 6]
)
trace2 = go.Scatter(
    x=[20, 30, 40],
    y=[50, 60, 70],
    xaxis='x2',
    yaxis='y2'
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        domain=[0, 0.7]
    ),
    xaxis2=dict(
        domain=[0.8, 1]
    ),
    yaxis2=dict(
        anchor='x2'
    ),
    shapes=[
        {
            'type': 'line',
            'xref': 'x',
            'yref': 'y',
            'x0': 2,
            'x1': 2,
            'y0': 3,
            'y1': 8
        },
        {
            'type': 'line',
            'xref': 'x2',
            'yref': 'y2',
            'x0': 25,
            'x1': 25,
            'y0': 40,
            'y1': 80
        }
    ]
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)

Hope that helps!
-Jon

2 Likes