Search code examples
pythonplotlyplotly-python

Remove repeated years from x-axis in plotly


I have draw a bar chart in plotly (python). The year value 2019 is repeated along y-axis. How can I display only one 2019 in the middle not others.

enter image description here

My code:

        a = pd.DataFrame(a.groupby(by=['Year', 'Vendor Name']) 
        ['Spend'].sum())
       
        fig_2 = px.bar(a, x='Year', y='Spend', 
        color='Vendor Name', barmode='stack', text='Spend')
        fig_2.update_traces(marker_line_width=0)
        fig_2.update_layout(
                    title="",
                    xaxis_title="",
                    yaxis_title="Total Spend",
                    legend_title="Vendor names",
                    height=450,
                    width=700,
                    
                    )

Solution

  • you can do

    a['year'] = a['date'].dt.year
    

    gives the column with only the year part, the actual date column should be of datetime though. Then maybe you could do

    fig2.update_xaxez(type="category")