Go.Candlestick crams some charts into the left side

Iā€™m using Go.Candlestick to plot intraday stock charts. Some charts will stretch all the way along the a-axis, but others will get crammed into the left side.

For the stock SNOA I have a df where the first 15 rows look like this:

                       Time  Open  High   Low  Close    Volume      n
0  2023-01-25 12:30:00+00:00  1.28  1.45  1.28   1.45    2502.0    8.0
1  2023-01-25 12:31:00+00:00  1.49  1.94  1.45   1.83   31671.0  113.0
2  2023-01-25 12:32:00+00:00  1.79  2.45  1.79   2.17  105012.0  649.0
3  2023-01-25 12:33:00+00:00  2.19  2.30  2.03   2.03  107070.0  650.0
4  2023-01-25 12:34:00+00:00  2.03  2.07  1.70   1.74  202442.0  946.0
5  2023-01-25 12:35:00+00:00  1.74  1.98  1.67   1.91  187701.0  978.0
6  2023-01-25 12:36:00+00:00  1.90  2.00  1.81   1.90  155680.0  844.0
7  2023-01-25 12:37:00+00:00  1.90  1.90  1.76   1.80   85694.0  532.0
8  2023-01-25 12:38:00+00:00  1.79  1.80  1.75   1.75   38054.0  260.0
9  2023-01-25 12:39:00+00:00  1.75  1.77  1.71   1.75   54587.0  336.0
10 2023-01-25 12:40:00+00:00  1.76  1.89  1.66   1.85  156428.0  607.0
11 2023-01-25 12:41:00+00:00  1.84  1.94  1.83   1.92  180898.0  802.0
12 2023-01-25 12:42:00+00:00  1.91  1.94  1.86   1.90  140119.0  539.0
13 2023-01-25 12:43:00+00:00  1.91  2.04  1.88   1.98  162479.0  775.0
14 2023-01-25 12:44:00+00:00  1.97  2.03  1.96   2.01  184726.0  767.0

Which prints well:

Then I have the stock AMV where the first 15 rows look like this:

                        Time  Open  High   Low  Close    Volume      n
0  2023-01-25 12:30:00+00:00  1.28  1.45  1.28   1.45    2502.0    8.0
1  2023-01-25 12:31:00+00:00  1.49  1.94  1.45   1.83   31671.0  113.0
2  2023-01-25 12:32:00+00:00  1.79  2.45  1.79   2.17  105012.0  649.0
3  2023-01-25 12:33:00+00:00  2.19  2.30  2.03   2.03  107070.0  650.0
4  2023-01-25 12:34:00+00:00  2.03  2.07  1.70   1.74  202442.0  946.0
5  2023-01-25 12:35:00+00:00  1.74  1.98  1.67   1.91  187701.0  978.0
6  2023-01-25 12:36:00+00:00  1.90  2.00  1.81   1.90  155680.0  844.0
7  2023-01-25 12:37:00+00:00  1.90  1.90  1.76   1.80   85694.0  532.0
8  2023-01-25 12:38:00+00:00  1.79  1.80  1.75   1.75   38054.0  260.0
9  2023-01-25 12:39:00+00:00  1.75  1.77  1.71   1.75   54587.0  336.0
10 2023-01-25 12:40:00+00:00  1.76  1.89  1.66   1.85  156428.0  607.0
11 2023-01-25 12:41:00+00:00  1.84  1.94  1.83   1.92  180898.0  802.0
12 2023-01-25 12:42:00+00:00  1.91  1.94  1.86   1.90  140119.0  539.0
13 2023-01-25 12:43:00+00:00  1.91  2.04  1.88   1.98  162479.0  775.0
14 2023-01-25 12:44:00+00:00  1.97  2.03  1.96   2.01  184726.0  767.0

which shows up as:

Full code:


def dataplotter(stock, date):
    # Create subplots and set plot grid size
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                   vertical_spacing=0.00, subplot_titles=(f'{stock}', ''), 
                   row_width=[0.2, 0.7])

    df = dataloader(stock, date)

    print(df.head(15))
    
    fig.add_trace(go.Candlestick(x=df['Time'],
                        open=df['Open'],
                        high=df['High'],
                        low=df['Low'],
                        close=df['Close'],
                                increasing_line_color= '#2196F3', decreasing_line_color= '#000000',
                                increasing_fillcolor= '#2196F3', decreasing_fillcolor= '#000000' ))

    fig.update_layout(height=900,
                        paper_bgcolor='rgba(211,211,211,211)', # Background outside
                        plot_bgcolor='rgba(255,255,255,255)')    # Background inside

    # Remove rangeslider + set hovermode (crosshair)
    fig.update_layout(xaxis_rangeslider_visible=False, xaxis=dict(dtick='M1'), hovermode='x unified')

    fig.update_yaxes(ticks="outside", tickwidth=1, tickcolor='grey', ticklen=10, col=1)

    # Add volume data in subplot
    fig.add_trace(go.Bar(x=df.Time, y=df.Volume, yaxis='y2'))

    fig.update_yaxes(gridcolor='rgba(211,211,211,211)', gridwidth=0.1, showspikes=True, spikedash='dot', spikemode='across', 
                    spikecolor="black",spikesnap="cursor",spikethickness=1)

    #fig.update_xaxes(showline=True, linewidth=2, linecolor='black')
    fig.update_yaxes(showline=True, linewidth=2, linecolor='black')
    
    fig.add_vline(x="2023-01-25 14:30:00+00:00", line_color='grey', line_dash="dash")
    
    fig.show()

    #Storage
    time_string = datetime.datetime.now().strftime('%Y-%m-%d %H.%M')
    filename = stock + ' ' + time_string + '.png'
    print(filename)
    fig.write_image(filename, scale=6, width=1920, height=1080)

Data = [['AMV', '2023-01-04'],
        ['SNOA', '2023-01-25']]

for instance in Data[0:2]:
    #print(instance[0], instance[1])
    dataplotter(instance[0], instance[1])

UPDATE
I had hardcoded in a vertical line on a specific date - which made any charts for other dates have an x-axis that stretched to include this date. Simple mistake.

1 Like