How to remove trailing spaces from right and left of a plotly chart?

Hello There,

Sorry if this this is not the 1st time this question is asked.

I just began using Plotly with Dash and i noticed category line charts have a space at the left and right of the chart. I would like to remove that space. My chart is has Timestamps on X axis and some values on the Y axis. I read the data for X and Y from two Pandas dataframe columns.

I guess what i want is to start the chart directly from the axis without a space). So this would mean that the first (x,y) pair to start directly from the axis like below:

time_axis_x=df[β€œDateTime”]

x_axis_dict = dict(
        type='category',
        title='Test',
        #nticks=int(len(time_axis_x)),
        automargin=True,
        tickangle=270,
        showgrid=True,
        #range=[time_axis_x[0], time_axis_x[len(time_axis_x)-1]],
        range=[0, len(time_axis_x)-1],

    )
data=[]
        trace = go.Scatter(
            x=time_axis_x,
            y=df["column0"],
            text='',
            mode='markers+lines',
            marker={'size': 6, 'line': {'width': 0.5, 'color': 'red'}},
            showlegend=True,
            connectgaps=True
        )
        data.append(trace)

    layout = go.Layout( xaxis=x_axis_dict,
                        yaxis=dict(title='Y title'),
                        title='Test',
                        title_x=0.5,
                        hovermode='x',
                        autosize=True,
                        margin=go.layout.Margin(
                           l=0,  # left margin
                           r=0,  # right margin
                           b=0,  # bottom margin
                           t=40,  # top margin
                       ),
                        legend=dict(orientation='h', yanchor="bottom", y=-1, xanchor="left")
                        )

fig = go.Figure(data=data, layout=layout)
fig.show()

1 Like

Could someone please help answer this question? I also have the same question.

Thanks.

Hi @kevinyin9 welcome to the forums.

You can change the xaxis autorange to a range that fits your needs.

fig.update_xaxes(range=[start, end])
1 Like

I am having the same issue as op. This solution does not fix it.

visual = pgo.Scatter(x=vacptimestamps, y=showSet.Visual, mode='markers+lines', name='Visual')
auditory = pgo.Scatter(x=vacptimestamps, y=showSet.Auditory, mode='markers+lines', name='Auditory')
cognitive = pgo.Scatter(x=vacptimestamps, y=showSet.Cognitive, mode='markers+lines', name='Cognitive')
FM = pgo.Scatter(x=vacptimestamps, y=showSet.FineMotor, mode='markers+lines', name='Fine <br>Motor')
speech = pgo.Scatter(x=vacptimestamps, y=showSet.Speech, mode='markers+lines', name='Speech')
tactile = pgo.Scatter(x=vacptimestamps, y=showSet.Tactile, mode='markers+lines', name='Tactile')
FVACP = pgo.Figure(data=[visual, auditory, cognitive, FM, speech, tactile],
                                layout=pgo.Layout(xaxis=dict(range=[vacptimestamps[0], vacptimestamps[-1]],