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:
-
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 -
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.