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