Trace not showing when second y axis added in subplots

I am trying to make 3x4 subplots where 3rd row had secondary axis, but, for some reasons, data that i added on that axes is not showing.

The code is like:

import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly

def test():
    fig = make_subplots(rows=3, cols=4, start_cell="top-left", shared_xaxes=True, shared_yaxes=True,
                        subplot_titles=('1st', '2nd', '3rd', '4th', '', '', '', '', '', '', '', ''))
    fig.update_xaxes(showline=True, linewidth=2, linecolor='gray', ticks="outside", tickcolor='gray')
    fig.update_yaxes(showline=True, linewidth=2, linecolor='gray', ticks="outside", tickcolor='gray')
    fig.update_layout(
        paper_bgcolor='rgba(255,255,255,1.0)',
        plot_bgcolor='rgba(255,255,255,1.0)'
    )
    for k in range(13, 17):
        fig.update_layout({'yaxis{}'.format(k): dict(anchor='x' + str(k-4),
                                                     overlaying='y'+str(k-4),
                                                     side='right',
                                                     showline=True,
                                                     linewidth=2,
                                                     linecolor='gray',
                                                     ticks="outside",
                                                     tickcolor='gray',
                                                     matches='y13'
                                                     )
                              })

    x_list = np.arange(0,10, 1)
    for i in range(4):
        trace1 = dict(
            x=x_list,
            y=np.random.rand(10),
            line=dict(
                color='#39B850'
            ),
            name='1 - ' + str(i)
        )
        trace2 = dict(
            x=x_list,
            y=np.random.rand(10),
            name='2 - ' + str(i),
            line=dict(
                color='#4543B9'
            )
        )
        trace3 = dict(
            x=x_list,
            y=np.random.rand(10),
            name='3 - ' + str(i),
            line=dict(
                color='#616BD9',
                dash='dot'
            )
        )
        trace4 = dict(
            x=x_list,
            y=np.random.rand(10),
            name='4 - ' + str(i),
            line=dict(
                color='#9F36AE'
            )
        )
        trace5 = dict(
            x=x_list,
            y=np.random.rand(10)*3,
            yaxis='y' + str(i+13),
            name='5 - ' + str(i),
            line=dict(
                color='#C76AD4',
                dash='dot'
            )
        )
        fig.add_trace(trace1, row=1, col=i+1)
        fig.add_trace(trace2, row=2, col=i+1)
        fig.add_trace(trace3, row=2, col=i+1)
        fig.add_trace(trace4, row=3, col=i+1)
        fig.add_trace(trace5)
    fig.show()

Traces “5 - 0”, … , “5 - 3” were added to their axes and showed in the legend, but they aren’t visible on the subplots. Why and how do i fix it?

@Mikewell

  1. To disable the default plotly template, you don’t need to perform layout updates, as you did in you code.
    The simplest solution is to set is as ‘none’:
import plotly.io as pio
pio.templates.default = "none"
  1. You worked with the older rules to create subplots.
    Supposing you are working with plotly 4.0 or 4.1.0 (and it seems that the case as long as you imported plotly.subplots), just add specs in make_subplots, like in the code below. Also until your code does not work, it is recommented to insert
    print_grid=True in the make_subplots, to see what xaxes, yaxes are assigned to each subplot cell.
    Here is you code modified to work:
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=3, cols=4, start_cell="top-left", shared_xaxes=True, shared_yaxes=True,
                        subplot_titles=('1st', '2nd', '3rd', '4th', '', '', '', '', '', '', '', ''),
                         print_grid=True,
                        specs=[[{"secondary_y": True}]*4]*3)
    

    

x_list = np.arange(10)
for i in range(4):
    trace1 = dict(
            x=x_list,
            y=np.random.rand(10),
            line=dict(
                color='#39B850'
            ),
            name='1 - ' + str(i)
        )
    trace2 = dict(
            x=x_list,
            y=np.random.rand(10),
            name='2 - ' + str(i),
            line=dict(
                color='#4543B9'
            )
        )
    trace3 = dict(
            x=x_list,
            y=np.random.rand(10),
            name='3 - ' + str(i),
            line=dict(
                color='#616BD9',
                dash='dot'
            )
        )
    trace4 = dict(
            x=x_list,
            y=np.random.rand(10),
            name='4 - ' + str(i),
            line=dict(
                color='#9F36AE'
            )
        )
    trace5 = dict(
            x=x_list,
            y=np.random.rand(10)*3,
            name='5 - ' + str(i),
            line=dict(
                color='#C76AD4',
                dash='dot'
            )
        )
    fig.add_trace(trace1, row=1, col=i+1,  secondary_y=False)
    fig.add_trace(trace5, row=1, col=i+1,  secondary_y=True)
    fig.add_trace(trace2, row=2, col=i+1,  secondary_y=False)
    fig.add_trace(trace3, row=2, col=i+1,  secondary_y=False)
    fig.add_trace(trace5, row=2, col=i+1,  secondary_y=True)
    fig.add_trace(trace4, row=3, col=i+1,  secondary_y=False)
    fig.add_trace(trace5, row=3, col=i+1,  secondary_y=True)
        
        
fig.show()
  1. Note, that spec are defined in compact form in make_subplots. If you are running
[[{"secondary_y": True}]*4]*3

you’ll get:

[[{'secondary_y': True},
  {'secondary_y': True},
  {'secondary_y': True},
  {'secondary_y': True}],
 [{'secondary_y': True},
  {'secondary_y': True},
  {'secondary_y': True},
  {'secondary_y': True}],
 [{'secondary_y': True},
  {'secondary_y': True},
  {'secondary_y': True},
  {'secondary_y': True}]]

i.e. each subplot cell has a secondary yaxis.

1 Like

Thank you very much!

1 Like
three reasons why you should buy plotly pro: support open source, get great support, host your plots and dashboards online