Flexible Population Pyramid Charts with positiv axis

Hi,
i am using the following code to generate different population pyramids:

# 4.7 Pyramid-Graph
@callback(
   Output(component_id='pyramid_graph', component_property='figure'),
   [Input(component_id='year_map_gem', component_property='value')],
   [Input(component_id='name_klick', component_property='value')],
   prevent_initial_call=False
)
def update_pyramid(jahr, gemeindeName):
   if jahr is not None:
       dff_01 = df_04[(df_04["Jahr"] == (jahr)) &
                      (df_04["Name"] == (gemeindeName))]
       fig = gp.Figure()
       name1 = 'Männer'
       name2 = 'Frauen'
       customdata = np.stack(
           (dff_01['Alter'], dff_01['männlich'], dff_01['weiblich']), axis=-1)
       hovertemp = '<br>Gruppe: %{meta[0]}<br>Alter: %{customdata[0]}<br> in dieser Altersklasse <br>Anzahl Männer:%{customdata[1]}<br>Anzahl Frauen :%{customdata[2]}'
       # Adding Male data to the figure
       fig.add_trace(gp.Bar(y=dff_01['Alter'], x=dff_01['männlich'],
                            name=name1,
                            orientation='h',
                            meta=[name1, dff_01['männlich']],
                            ))
       # Adding Female data to the figure
       fig.add_trace(gp.Bar(y=dff_01['Alter'], x=dff_01['weiblich'] * -1,
                            name=name2, orientation='h',
                            meta=[name2, dff_01['weiblich']],
                            ))
       # Updating the layout for our graph
       fig.update_layout(title='Bevölkerungspyramide im Jahr ' + str(jahr) + ' für ' + gemeindeName,
                         title_font_size=20, barmode='relative',
                         bargap=0.0, bargroupgap=0,
                         yaxis=gp.layout.YAxis(title='Alter'),
                         xaxis=gp.layout.XAxis(title='Anzahl', showtickprefix='none')
                         )

       fig.update_traces(customdata=customdata, hovertemplate=hovertemp)
       return fig
   elif jahr is None:
       raise dash.exceptions.PreventUpdate

This is the result:

I have 2 problems with the result:

  1. Negative values in the x-axis
    I can not use ticktext to solve this since the axis-Range is flexible and variates between 10 to 100 000

  2. I underlined in the screenshot -20 because hier -20 is actually - 20 000 and i would like it to be shown in constant manner. Here I have the same problem like above: Because the axis is flexible I can not fix it.

Thank you.

hej lovitas,

I had the same issue regarding the flexible axis-range. Although I came up with a work-around in RStudio, it shoud work in your case as well. Furthermore this work-around will adress your first problem:

Firstly, I defined a function which takes a dataframe.series as input. The input will be the min number of the dynamically changing dataframe.series. The output will be a list with specific distance:

def getList(pd.Series = x):
  int z = min(x)
  
 if(abs(z) < 1000):
    z = round(z/100)*100
  
 else if(abs(z) >= 1000 AND abs(z) < 10000):
    z = round(z/1000)*1000
  
 else if(abs(z > 10000) AND abs(z) < 100000):
    z = round(z/10000)*10000
 
  length <- abs(2*z)/4
  val_ticks <- list(range(z,abs(z), length))
  return val_ticks
}

example:

* getPositivList(-260): [-300, -150,  0,  150, 300]

you might change the steps in the range function by editing the length

Then apply function to fig.update_layout:

fig.update_layout(
    xaxis = dict(
        tickmode = 'array',
        tickvals = getList(dff_01['weiblich']),
        ticktext = abs(getList(dff_01['weiblich']))
    )
)

I hope this is somewhat helpful for your problem.