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

Group bar chart with secondary y axis in plotly by python

ar_data = go.Bar(
          x = compa1.index,
          y = sales1.groupby('PRODUCTLINE')['SALES'].sum(),
          name = 'Total Sales',
          text = sales1.groupby('PRODUCTLINE')['SALES'].sum(),
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          marker = dict(color='rgb(187, 204, 46)'),
          yaxis = 'y1'
)

line_data = go.Bar(
          x = compa1.index,
          y = sales1.groupby('PRODUCTLINE')['QUANTITYORDERED'].sum(),
          name = 'Total Quantity Ordered',
          text = sales1.groupby('PRODUCTLINE')['QUANTITYORDERED'].sum(),
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          marker = dict(color='rgb(46, 204, 154 )'),
          yaxis = 'y2'
) 



data = [bar_data, line_data]

layout = go.Layout(
    barmode='group',
    title = 'Total sales and quantity ordered',
    hovermode = 'closest',
    
    xaxis=dict(title='Name of Product',
                showline=True,
                showgrid=False,
                showticklabels=True,
                linecolor='rgb(104, 204, 104)',
                linewidth=2,
                ticks='outside',
         tickfont=dict(
                family='Arial',
                size=12,
                color='rgb(82, 82, 82)',
        ),
               
    ),
    yaxis=dict(title='Total Sales'),
    yaxis2=dict(title='Total quantity ordered', side='right',overlaying='y'),
    
     legend=dict(
        orientation='v',
        bgcolor='rgba(255, 255, 255, 0)',
        bordercolor='rgba(255, 255, 255, 0)',
        x=0.79,
        y=0.97,
        traceorder="normal",
        font=dict(
            family="sans-serif",
            size=12,
            color="black"
        ),
    )
    
)

figure2 = go.Figure(data=data, layout=layout)
figure2.update_layout(uniformtext_minsize=10, uniformtext_mode='hide')
figure2.update_layout(legend_title='<b> Trend </b>')

figure2.update_layout(
    title_font_family="Times New Roman",
    title_font_color="red",
    title_font_size=30,
    legend_title_font_color="green")

figure2.update_layout(
    title={
        'text': 'Total sales and quantity ordered',
        'y':0.9,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'})


figure2.show()

it shows two legends but displays

only single bar

Please fix error in the above mentioned code. Chart displays only one bar

Hi jawabutt
Something is wrong with your data.
I take your code, with simple data and it works:

bar_data = go.Bar(
          x = [1, 4, 6],
          y = [22, 25, 28],
          name = 'Total Sales',
          yaxis = 'y1'
)

line_data = go.Bar(
          x = [2, 5, 7],
          y = [40, 50, 60],
          yaxis = 'y2'
) 



data = [bar_data, line_data]

layout = go.Layout(
    barmode='group',
    title = 'Total sales and quantity ordered',
    hovermode = 'closest',
    
    xaxis=dict(title='Name of Product',
                showline=True,
                showgrid=False,
                showticklabels=True,
                linecolor='rgb(104, 204, 104)',
                linewidth=2,
                ticks='outside',
         tickfont=dict(
                family='Arial',
                size=12,
                color='rgb(82, 82, 82)',
        ),
               
    ),
    yaxis=dict(title='Total Sales'),
    yaxis2=dict(title='Total quantity ordered', side='right',overlaying='y'),
    
     legend=dict(
        orientation='v',
        bgcolor='rgba(255, 255, 255, 0)',
        bordercolor='rgba(255, 255, 255, 0)',
        x=0.79,
        y=0.97,
        traceorder="normal",
        font=dict(
            family="sans-serif",
            size=12,
            color="black"
        ),
    )
    
)

figure2 = go.Figure(data=data, layout=layout)
figure2.update_layout(uniformtext_minsize=10, uniformtext_mode='hide')
figure2.update_layout(legend_title='<b> Trend </b>')

figure2.update_layout(
    title_font_family="Times New Roman",
    title_font_color="red",
    title_font_size=30,
    legend_title_font_color="green")

figure2.update_layout(
    title={
        'text': 'Total sales and quantity ordered',
        'y':0.9,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'})


figure2.show()

Image

Thanks for you reply.

bar_data = go.Bar(
          x = compa1.index,
          y = sales1.groupby('PRODUCTLINE')['SALES'].sum(),
          name = 'Total Sales',
          text = sales1.groupby('PRODUCTLINE')['SALES'].sum(),
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          marker = dict(color='rgb(214, 137, 16)'),
          yaxis = 'y1',
          offsetgroup=1
)

line_data = go.Bar(
          x = compa1.index,
          y = sales1.groupby('PRODUCTLINE')['QUANTITYORDERED'].sum(),
          name = 'Total Quantity Ordered',
          text = sales1.groupby('PRODUCTLINE')['QUANTITYORDERED'].sum(),
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          marker = dict(color='rgb(112, 123, 124)'),
          yaxis = 'y2',
          offsetgroup=2
) 

Now this code work correctly by adding two properties in two bar data.
offsetgroup=1
offsetgroup=2