How to plot Multiple Candlesticks by Month and Day over same x axis with Python Plotly?

Viewed 375

I'm trying to plot multiple candlesticks (from a single dataframe) over the same x axis (Jan01-Dec31).

For example, I got the last 5 years of stock data for one specific stock and I want to plot a Candlestick for each year, plotted on top of each other, over the same x axis (Jan01-Dec31).

Axis would look like:
x axis: Jan01,jan02...Dec31
y axis: Year1, Year2,...YearN data as Candlesticks on top of each other over the same x axis.

I'm pulling stock data from Yahoo Finance using pandas_datareader.get_data_yahoo() over a period of many years. I added two auxiliary columns with dates in different formats to try to show x axis with Month and Day, and still produce the multiple plots over the same x axis.
Here is a sample from my dataframe with added columns date2 and date3
Sample Dataframe

To plot, I divided the DataFrame by year, and each year is one object of type Candlestick from plotly.graph_objects.

I got stuck between two undesirable choices:

1 - Using df['date3'] = df1.index.strftime("%b %d") as x axis (converts to month and day like Jan01).
The problem is if I try to convert the date to month and day, excluding year, the resulting graph appends all dates on the x axis. The x axis, instead of the sequence Jan01-Dec31, is appending all dates like Jan01-Dec31Jan01-Dec31Jan01-Dec31. Wrong graph with labels as month and day

2 - Using df['date2'] = df1.index.strftime("%j") as x axis(convert to Day of the Year like 1-365). Using the x axis as a number from 1-365, I can plot the graph exactly like I want, but x axis doesn't show Month and Day, as it's showing 1-365 Right graph but wrong x axis labels(1-365 instead of Jan01-Dec31)

3 - Through a similar topic with a different context on Stackoverflow(Python - Plot Multiple Dataframes by Month and Day (Ignore Year)), I found a possible solution on the plotly documentation(https://plotly.com/python/tick-formatting/#tickmode--array), but that didn't work for me. The idea is to update the tick values and text like this:

fig.update_layout(
    xaxis = dict(
        tickmode = 'array',
        tickvals = [1, 3, 5, 7, 9, 11],
        ticktext = ['One', 'Three', 'Five', 'Seven', 'Nine', 'Eleven']
    )
)

This is my code to generate 1 Candlestick object:

plotObj = go.Candlestick(
    x=dfResult[start_index:end_index]['date2'],
    xaxis=dict(
        tickmode='array',
        tickvals=dfResult[start_index:end_index]['date2'],
        ticktext=dfResult[start_index:end_index]['date3']
    ),
    open=dfResult[start_index:end_index]['Open'],
    high=dfResult[start_index:end_index]['High'],
    low=dfResult[start_index:end_index]['Low'],
    close=dfResult[start_index:end_index]['Close'],
    name="%s" % trace_label
)

This is the error I get when I try to run the code above (adding xaxis=dict()):

ValueError: 
    Invalid value of type 'builtins.dict' received for the 'xaxis' property of candlestick
        Received value: {'tickmode': 'array', 'tickvals': 0     308
1     309
2     310
3     313
4     314
5     315
6     316
7     317
8     320
9     321
10    322
11    323
12    324
13    327
14    328
15    329
16    331
17    334
18    335
19    336
20    337
21    338
22    341
23    342
24    343
25    344
26    345
27    348
28    349
29    350
30    351
31    352
32    355
33    356
34    357
35    358
36    362
37    363
38    364
39    365
Name: date2, dtype: int64, 'ticktext': 0     Nov 04
1     Nov 05
2     Nov 06
3     Nov 09
4     Nov 10
5     Nov 11
6     Nov 12
7     Nov 13
8     Nov 16
9     Nov 17
10    Nov 18
11    Nov 19
12    Nov 20
13    Nov 23
14    Nov 24
15    Nov 25
16    Nov 27
17    Nov 30
18    Dec 01
19    Dec 02
20    Dec 03
21    Dec 04
22    Dec 07
23    Dec 08
24    Dec 09
25    Dec 10
26    Dec 11
27    Dec 14
28    Dec 15
29    Dec 16
30    Dec 17
31    Dec 18
32    Dec 21
33    Dec 22
34    Dec 23
35    Dec 24
36    Dec 28
37    Dec 29
38    Dec 30
39    Dec 31
Name: date3, dtype: object}

    The 'xaxis' property is an identifier of a particular
    subplot, of type 'x', that may be specified as the string 'x'
    optionally followed by an integer >= 1
    (e.g. 'x', 'x1', 'x2', 'x3', etc.)
0 Answers
Related