Search code examples
pythonplotlyplotly-python

Align Plotly Bar Chart with X and Y Axis (Python)


I have the following chart that is not lining up with the axis. Does anyone know how to fix it?

enter image description here

Here is my code:

path = 'https://raw.githubusercontent.com/higor-gomes93/employee_attrition/main/employee_attrition.csv'
dataset = pd.read_csv(path)

colunas_bar_plot = dataset.nunique()[(dataset.nunique() < 50) & (dataset.nunique() > 10)].keys().tolist()
colunas_bar_plot = [x for x in colunas_bar_plot]

limite_superior = int(len(colunas_bar_plot)/2) if len(colunas_bar_plot)/2 == len(colunas_bar_plot)//2 else len(colunas_bar_plot)//2 + 1

fig = make_subplots(rows = limite_superior, cols = 2, subplot_titles = colunas_bar_plot)
contador = 0

for i in range(1, 3):
    for j in range(1, limite_superior+1):
        try:
            
            legenda = True if contador == 0 else False

         
            aux_1 = dataset_inativos
            aux_2 = dataset_ativos
            aux_3 = pd.DataFrame(pd.crosstab(dataset[colunas_bar_plot[contador]], dataset['Attrition']), )
            aux_3['% de Saídas'] = aux_3['Yes'] / (aux_3['Yes'] + aux_3['No']) * 100
            aux_3.columns = ['No', 'Yes', '% de Saídas']
            aux_3.index.name = None
            
            
            data = [aux_1, aux_2]
            labels = ['Inativos', 'Ativos']
            colors = ['#FFA07A', '#90EE90']

            
            fig.add_trace(go.Bar(x = aux_1[colunas_bar_plot[contador]].value_counts().keys().tolist(), 
                                    y = aux_1[colunas_bar_plot[contador]].value_counts().values.tolist(), 
                                    name = 'Inativos', 
                                    opacity = 0.8,
                                    showlegend = legenda,
                                    marker = dict(color = 'LightSalmon', line = dict(color = '#000000', width = 1))), 
                            row = j, col = i)
            fig.add_trace(go.Bar(x = aux_2[colunas_bar_plot[contador]].value_counts().keys().tolist(), 
                                    y = aux_2[colunas_bar_plot[contador]].value_counts().values.tolist(), 
                                    name = 'Ativos', 
                                    opacity = 0.8,
                                    showlegend = legenda, 
                                    marker = dict(color = 'LightGreen', line = dict(color = '#000000', width = 1))), 
                            row = j, col = i)
            fig.add_trace(go.Scatter(x = aux_3.index, 
                                        y = aux_3['% de Saídas'], 
                                        name = '% de Saídas', 
                                        opacity = 0.6, 
                                        showlegend = legenda,
                                        marker = dict(color = 'black', line = dict(color = '#000000', width = 0.5))), 
                            row = j, col = i)
            contador += 1
        except:
            pass

fig.update_layout(width = 1500, height = 2000, title = "Barras das variáveis numéricas de interesse", plot_bgcolor = "white", title_x = 0.5)
fig.update_yaxes(showline = True, linewidth = 1, linecolor = 'black', secondary_y = False)
fig.update_xaxes(showline = True, linewidth = 1, linecolor = 'black')
fig.update_xaxes(showgrid = True, gridwidth = 1, gridcolor = '#EEEEEE')
fig.show()

P.s.: dataset, dataset_inativos and dataset_ativos are previously defined; i took a screen shot of one of the charts, since I am making severel subplots (some other charts from this grid are not lining up either)


Solution

  • As it turns out, this can be solved by using rangemode = 'tozero' inside the update_xaxes method.