Search code examples
pythonplotly

Plotly waterfall chart not showing total


i have data i wish to represent in a plotly waterfall chart. However, it does chart the total column. this is the the case:

x_values=['2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', 'i alt']
y_values=[4559, 3080, 2988, 2979, 6668, 6492, 3416, 6122, 7709, 2076, 4676, 50766]
wf_measure=['relative', 'relative', 'relative', 'relative', 'relative', 'relative', 'relative', 'relative', 'relative', 'relative', 'relative', 'total']

fig2 = go.Figure(go.Waterfall(name = "", orientation = "v",
                            measure = wf_measure,
                            x = x_values,
                            textposition = "outside",
                            text = y_values,
                            y = y_values,
                            connector={"line": {"color": "black", "width": 1, "dash": "dot"}},
                            texttemplate='%{text:,.0f}M'))
        
fig2.update_layout(title = 'dette er en test',
                title_x=0.25,
                title_font=dict(size=16),
                xaxis_title="År",
                showlegend = False,
                height=450, 
                width=1020,
                plot_bgcolor='white',
                xaxis=dict(showgrid=True, gridcolor='lightgrey', gridwidth=0.5, tickvals=x_values),
                yaxis=dict(showgrid=True, gridcolor='lightgrey', gridwidth=0.5, tickformat='.0f'),
                shapes=[{'type':'line',
                            'x0':x_values[0],
                            'x1':x_values[-1],
                            'y0':0,
                            'y1':0,
                            'line':{'color':'grey','width':0.5,'dash':'dash'}}]
                )
        
fig2.update_traces(
    textfont=dict(size=10), 
    decreasing=dict(marker=dict(color='#e65a6d')), 
    increasing=dict(marker=dict(color='#a3dce8')), 
    totals=dict(marker=dict(color='#003755'))
    )

fig2.show()  

Attached is a output png. does anybody have a suggestion?

fig2


Solution

  • When I ran your code, the x-axis did not display correctly and the totals were not displayed before. When I isolated the issue, it was affected by the addition of the line. Without setting that up, I specified the line width and color in the axis customization. I was convinced this was the graph you were looking for.

    fig2 = go.Figure(go.Waterfall(name = "", orientation = "v",
                                measure = wf_measure,
                                x = x_values,
                                textposition = "outside",
                                text = y_values,
                                y = y_values,
                                connector={"line": {"color": "black", "width": 1, "dash": "dot"}},
                                texttemplate='%{text:,.0f}M'))
            
    fig2.update_layout(title = 'dette er en test',
                    title_x=0.25,
                    title_font=dict(size=16),
                    xaxis_title="År",
                    showlegend = False,
                    height=450, 
                    width=1020,
                    plot_bgcolor='white',
                    xaxis=dict(showgrid=True, gridcolor='lightgrey', gridwidth=0.5, tickvals=x_values),
                    yaxis=dict(showgrid=True, gridcolor='lightgrey', gridwidth=0.5, tickformat='.0f'),
                    # shapes=[{'type':'line',
                    #             'x0':x_values[0],
                    #             'x1':x_values[-1],
                    #             'y0':0,
                    #             'y1':0,
                    #             'line':{'color':'grey','width':0.5,'dash':'dash'}}]
                    )
    fig2.update_xaxes(showline=True, linewidth=0.5, linecolor='grey') # update
    
    fig2.update_traces(
        textfont=dict(size=10), 
        decreasing=dict(marker=dict(color='#e65a6d')), 
        increasing=dict(marker=dict(color='#a3dce8')), 
        totals=dict(marker=dict(color='#003755'))
        )
    fig2.update_yaxes(range=[np.array(y_values).min(), np.array(y_values).max()+5000]) #update
    fig2.show()
    

    enter image description here