Can you help me achieve similar to the image? What I want is the date will be separated by a vertical line that will act as boundary between two months and possibly the month is located on the center?
In your code with the x-axis setup, doesn’t the month name appear under the day when the month changes? The result is that the next month is recognized as having changed, but the x-axis splitting feature seems to work in the multi-index case and is not applicable in this case. Reference
Yes when I add the month, the month text would appear below the date, but what I want to achieve is the “Jun” text would be below the date 06 and the “May” text would be below around the date “21”
The only way left is to make your own scale. There may be several ways to do this, but you can create a list of day and month strings in the date string data of the data frame. Leave only the month name in the center position in the month list. Rejoin it with the list of days and a line break tag. To apply this, graph the x-axis of the original graph with integer indices and then update the tick marks.
ticks_day = [ x.strftime('%d-%b').split('-')[0] for x in df.index]
ticks_month = [ x.strftime('%d-%b').split('-')[1] for x in df.index]
may = ticks_month.count('May')
jun = ticks_month.count('Jun')
import math
new_month = [ t if i in [math.floor(may/2), may+math.floor(jun/2)] else '' for i,t in enumerate(ticks_month)]
new_ticks = [d+'<br>'+m for d,m in zip(ticks_day, new_month)]
fig.add_trace(go.Scatter(
mode='markers+lines',
x=np.arange(len(df)), #df.index,
y=df['Close']
))
fig.update_xaxes(tickvals=np.arange(len(df)),ticktext=new_ticks)