Why when using fig.update_traces(xaxis='x1') doesn't print axis tick and labels bellow second subplot?

Viewed 34

When I use "fig.update_traces(xaxis='x1')" it doesn't show dates on x-axis bellow second subplot. This line of code is used to extended line hover to all subplots. If I comment this line, then it works ok. Does anyone have an idea what it could be?

import yfinance as yf
import plotly.graph_objects as go
from plotly.subplots import make_subplots  

ticker = "AAPL"
df = yf.download(ticker, start="2021-01-01")
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.05, row_heights=[0.7, 0.3])
fig.add_trace(go.Candlestick(x=df.index,
                             open=df['Open'],
                             high=df['High'],
                             low=df['Low'],
                             close=df['Close'], 
                             name = ticker,
                             ), row=1, col=1)
colors = ['green' if (row['Close'] - row['Open']) >= 0 
          else 'red' for index, row in df.iterrows()]
fig.add_trace(go.Bar( x=df.index, 
                      y=df['Volume'],
                      marker_color=colors,
                      name = 'Volume',
                      hovertemplate = "Volume: %{y:,.0f}<extra></extra>",
                      ), row=2, col=1)
fig.update_layout(
        xaxis_rangeslider_visible=False,
        hovermode='x unified',
        xaxis2=dict(
              tickformat = '%d/%m/%Y',  # this controls the format of the x-value in the hover box
              ticks = "outside",
              type = "date",
              tickmode = "auto",
              ),   
        )

#  spike line hover extended to all subplots
fig.update_traces(xaxis='x1')
fig.show()
1 Answers

Hoverlines across subplots have not yet been implemented; they are up on github as an issue. (Incidentally, github is looking for sponsors.) My workaround is to unshare the x-axis and show the 1st x-axis.

import yfinance as yf
import plotly.graph_objects as go
from plotly.subplots import make_subplots  

ticker = "AAPL"
df = yf.download(ticker, start="2021-01-01")
fig = make_subplots(rows=2, cols=1, shared_xaxes=False, vertical_spacing=0.1, row_heights=[0.7, 0.3])
fig.add_trace(go.Candlestick(x=df.index,
                             open=df['Open'],
                             high=df['High'],
                             low=df['Low'],
                             close=df['Close'], 
                             name = ticker,
                             ), row=1, col=1)
colors = ['green' if (row['Close'] - row['Open']) >= 0 
          else 'red' for index, row in df.iterrows()]
fig.add_trace(go.Bar( x=df.index, 
                      y=df['Volume'],
                      marker_color=colors,
                      name = 'Volume',
                      hovertemplate = "Volume: %{y:,.0f}<extra></extra>",), row=2, col=1)

fig.update_layout(
        xaxis_rangeslider_visible=False,
        hovermode='x unified',
        xaxis2=dict(
              tickformat = '%d/%m/%Y',  # this controls the format of the x-value in the hover box
              ticks = "outside",
              type = "date",
              tickmode = "auto",
              ),   
        )

#  spike line hover extended to all subplots
fig.update_traces(xaxis='x1')

fig.show()

enter image description here

Related