Plotly Multi-Y Axis with same X axis time chart

Hi All!

Looking for some help on this:

I’m trying to create a chart with 2 Y-axis.

The first is a candlestick chart, the 2nd is a simple line chart.
Both have the same X axis.

When I plot this, the x-axis of the candlestick chart is covering up my line chart. Is there a way to force x-axis labels to be under the line graph instead?

    traces = []
    #yy/mm/dd H
    traces.append(go.Candlestick(
                            name = 'Candlestick',
                            x=df['x'],
                            open=df.Open,
                            high=df.High,
                            low=df.Low,
                            close=df.Close))
## Y2
traces.append(go.Scatter(x=df['x'], y=k['data'], mode='lines',name=k['name'], showlegend=False, yaxis="y2"))

Layout:

                    title=agg,
                    margin={'l': 10, 'r': 10, 't': 40, 'b': 10},
                    yaxis={
                        "domain":[.3, 1]
                    },
                    yaxis2={
                        "side": "right",
                        "domain": [0, 0.3]
                    },
                    xaxis={
                        "type": 'category', 
                        "categoryorder": 'category ascending',
                        "showspikes": True,
                        "tickangle" : 45,
                        "range": [s-50,s]
                    },
                    uirevision = symbol,    
                    showlegend=True,
                    legend_orientation="h",
                    legend=dict(x=.25, y=1.1),              
                )

The last example on here https://plot.ly/python/subplots/ " Stacked Subplots with a Shared X-Axis" is what I’m looking to replicate.

Moving this to API, Python as it would make more sense.

Thanks in advanced

@vantaka2 When you define fig=make_subplots() set vertical_spacing=a, with 0<a<1, such that to avoid overlapping of the two charts:

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime

fig = make_subplots(
            rows=2, cols=1,
            subplot_titles=("Title 1", "Title 2"),
            shared_xaxes=True,
            vertical_spacing =0.3)# tune this value until the two charts don't overlap


df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

I = [4*k for k in range(127)]

xx  =  df['Date'][I]
fig.add_trace(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'],
                high=df['AAPL.High'],
                low=df['AAPL.Low'],
                close=df['AAPL.Close']), row=1, col=1)

fig.add_trace(go.Scatter(x=xx, y= np.random.rand(len(I)), mode='lines'), row=2, col=1)
fig.update_layout(width=800)
fig.show()

1 Like

Thank you very much :heart_eyes: