Learn how to use Dash Bio for next-gen sequencing & quality control. 🧬 Register for the Oct 27 webinar.

Plotly Dash - Different Y Axis

Hi,

I am trying to set different y axis for these graphs. I have accomplished that via secondary_y parameter but with this approach I struggle to add different axis for Bar chart.

Would you please have any idea?

Thanks

from yahooquery import Ticker
import plotly.graph_objects as go
from plotly.subplots import make_subplots

def volumeGraph(ticker, start_date, end_date, value_check):
    stock = Ticker(ticker)
    average = stock.summary_detail[ticker]['twoHundredDayAverage']
    print("new value check {}".format(value_check))

    start = start_date
    end = end_date

    data = []

    # TODO https://plotly.com/python/time-series/
    df = stock.history(start=start, end=end, interval='1d')

    # Create figure with secondary y-axis
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    moving = go.Scatter(x=[df.index.date.min(), df.index.date.max()], y=[average, average], mode="lines",
                        name="52w Average")

    # Traces
    close = go.Scatter(x=df.index.date,
                   y=df['close'],
                   name="Close Price",
                   line=dict(color='#39c1b5'),

    )

    volume = go.Bar(x=df.index.date,
               y=df['volume'],
               name="Volume"
    )

    layout = go.Layout(title=ticker)

    data.append(close)
    data.append(volume)
    print(data)

    

    fig = go.Figure(data=data, layout=layout)

    return fig```

This is solution I did from few minutes ago

from yahooquery import Ticker
import plotly.graph_objects as go
from plotly.subplots import make_subplots


def volumeGraph(ticker, start_date, end_date, value_check):
    stock = Ticker(ticker)
    average = stock.summary_detail[ticker]['twoHundredDayAverage']
    print("new value check {}".format(value_check))

    start = start_date
    end = end_date

    # TODO https://plotly.com/python/time-series/
    df = stock.history(start=start, end=end, interval='1d')

    # Create figure with secondary y-axis
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    # Traces
    close = go.Scatter(x=df.index.date,
                       y=df['close'],
                       name="Close Price",
                       line=dict(color='#39c1b5'),

                       )

    volume = go.Bar(x=df.index.date,
                    y=df['volume'],
                    name="Volume"

                    )

    moving = go.Scatter(x=[df.index.date.min(), df.index.date.max()], y=[average, average], mode="lines",
                        name="52w Average")

    layout = go.Layout(title=ticker)

    # data.append(close)
    # data.append(volume)
    # print(data)

    fig.add_trace(close, secondary_y=False)
    fig.add_trace(volume, secondary_y=True)

    if value_check:
        fig.add_trace(moving, secondary_y=False)
    else:
        pass

    # fig = go.Figure(data=data, layout=layout)

    return fig```

Is there better way to do it?