-1

I want to write a function to plot population pyramid but i don't know how to distinguish 2 parameters have same name, i want to write a function based on code below. There are 2 name, 'Unmarried Men' and 'Unmarried Women', and 2 title, 'Compare unmarried Men and Women', 'Population in thousands'.

fig = gp.Figure()

fig.add_trace(gp.Bar(y = df['Age'], x = df['Unmarried_Men'], name = 'Unmarried Men', orientation = 'h'))

fig.add_trace(gp.Bar(y = df['Age'], x = df['Unmarried_Women'] * -1, name = 'Unmarried Women', orientation = 'h'))

fig.update_layout(title = 'Compare unmarried Men and Women', 
                  barmode = 'overlay', 
                  bargroupgap = 0, 
                  xaxis = dict(tickvals = [-3000, -2000, -1000, 0, 1000, 2000, 3000], 
                               ticktext = ['-3000', '-2000', '-1000', '0', '1000', '2000', '3000'], 
                               title = 'Population in thousands'))

fig.show()
Phan
  • 11
  • 6
  • 1
    you have to give the parameters different names, you can't make a function that has two parameters with the same name. e.g. here you have the main title of the plot and the title of the x-axis, so you could have parameters named `title` and `x_axis_title` – Anentropic Jul 01 '22 at 16:46
  • I think i can't change name parameters so i will left 2 name default is Men and Women. @Anentropic – Phan Jul 01 '22 at 17:09

1 Answers1

1

Im try to write function based on code above and it's worked, thanks for your suggestions @Anentropic

def drawplot_2(y1, y2, x1, x2, title, xaxis_title):
    fig = gp.Figure()
    fig.add_trace(gp.Bar(y = y1, x = x1, name = 'Men', orientation = 'h'))
    fig.add_trace(gp.Bar(y = y2, x = x2 * -1, name = 'Women', orientation = 'h'))
    fig.update_layout(title = title, 
                      barmode = 'overlay', 
                      bargroupgap = 0, 
                      xaxis = dict(tickvals = [-3000, -2000, -1000, 0, 1000, 2000, 3000], 
                                   ticktext = ['-3000', '-2000', '-1000', '0', '1000', '2000', '3000'], 
                                   title = xaxis_title))
    fig.show()
Phan
  • 11
  • 6