Black Lives Matter. Please consider donating to Black Girls Code today.

Format secondary yaxis in a subplots.make_subplots to % (percentage) fig.update_yaxes

Hi Community,

new to Plotly and Dash

Doing a Covid-19 plot. Wanted to format the secondary y axis of the “bottom” chart to % value.

Is it a parameter to pass in fig.update_yaxes() ?

Anyone have a clue?

Code below and also a screenshot

import plotly.graph_objs as go
import plotly.offline as pyo
from plotly import subplots

data = {'x': ['2020-03-13', '2020-03-14', '2020-03-15', '2020-03-16',
                    '2020-03-17', '2020-03-18', '2020-03-19', '2020-03-20',
                    '2020-03-21', '2020-03-22', '2020-03-23', '2020-03-24',
                    '2020-03-25', '2020-03-26', '2020-03-27', '2020-03-28',
                    '2020-03-29', '2020-03-30', '2020-03-31', '2020-04-01',
                    '2020-04-02', '2020-04-03', '2020-04-04'], 
        'cases': [16,  19,  20,  29,  29,  37,  42,  50,  67, 100, 134, 170, 170,
                            235, 257, 287, 299, 305, 337, 367, 367, 396, 407], 
        'active': [13,  16,  17,  26,  26,  34,  39,  47,  64,  97, 131, 167, 164,
                            229, 251, 281, 293, 299, 331, 361, 361, 350, 361], 
        'recovered': [3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  6,  6,  6,  6,  6,
                            6,  6,  6,  6, 46, 46], 
        'deaths': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0], 
        'recovery_rate': [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
                                                        1., 1., 1., 1., 1., 1.]}


def plot_report(state='South Australia'):
    
    x = data['x']
    cases = data['cases']
    active = data['active']
    recovered = data['recovered']
    deaths = data['deaths']
    recoveryrate = data['recovery_rate']

    fig = subplots.make_subplots(rows=2, cols=1,
                                subplot_titles=(
                                    'Active and Confirmed Cases', 'Recovered Cases and Deaths'),
                                shared_xaxes=True,  
                                specs=[[{"secondary_y": False}],
                                        [{"secondary_y": True}]
                                        ]
                                )


    # top chart
    # add active cases

    fig.add_trace(
        go.Scatter(
            x=x,
            y=active,
            mode='lines',
            name='active cases'
        ), row=1, col=1, secondary_y=False
    )

    # add confirmed cases
    fig.add_trace(
        go.Scatter(
            x=x,
            y=cases,
            mode='lines',
            name='confirmed cases'
        ), row=1, col=1, secondary_y=False
    )


    # bottom chart
    # add recovered cases
    fig.add_trace(
        go.Bar(
            x=x,
            y=recovered,
            name='Recovered cases'
        ), row=2, col=1, secondary_y=False
    )

    # add deaths

    fig.add_trace(
        go.Bar(
            x=x,
            y=deaths,
            name='Deaths'
        ), row=2, col=1, secondary_y=False
    )

    # add recovery rate secondary Y axis

    fig.add_trace(
        go.Scatter(
            x=x,
            y=recoveryrate,
            mode = 'lines',
            name='recovery rate'

        ), row=2, col=1, secondary_y=True
    )

    fig.update_yaxes(range=[0, 1.1], row=2, col=1,
                     secondary_y=True)


    fig.update_layout(
        title_text=f"COVID-19 Situation Status Report for {state} ",
        width=900,
    )


    return fig 


fig = plot_report()

    
fig.show()