Home > Software design >  Plot bar chart with multiple columns plotly
Plot bar chart with multiple columns plotly

Time:07-07

I have a dataset like this:

Female    Male    Single    Married    Cohabit    OthSing
  1        0        1          0          0          0
  0        1        0          1          0          0
  0        1        0          0          0          1
  1        0        0          0          1          0
  0        1        1          0          0          0
  1        0        0          0          1          0

Where, basically, if you have 1 for Female, it's gonna be 0 for Male and vice versa. The same happens with the marital status, if the male/female is married, then it can't be single or cohabiting. I want to plot a bar chart using plotly that contains 4 bars (the marital status), and I don't care about if it's male or female, I just want to know how many people are single, married, cohabiting, or another kind of single. My problem is that each bar comes from a different column and I couldn't find a way to do that yet. Output example below:

desired output

Could someone give me an idea if it's possible to do that?

CodePudding user response:

Maybe for someone that needs that in the future, I managed to plot it by creating a list with the indexes of the columns I needed and another one with the counts for each column and then plotted the lists, like that:

index_mar_status = df_data.columns[2:6].values

count_mar_status = [df_data[mar_status].value_counts()[1] for mar_status in index_mar_status]

bar = px.bar(x=index_mar_status,
             y=count_mar_status,
             color=count_mar_status,
             color_continuous_scale=px.colors.sequential.Pinkyl,
             title='Count of Marital Status')

bar.update_layout(yaxis_title='Number of People',
                  coloraxis_showscale=False)
bar.show()

CodePudding user response:

Try this, using pandas plot:

ax = df.T[2:].sum(axis=1).plot.bar(rot=0)
ax.set_ylim(0,2.5)
ax.grid(axis='y')
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

Output:

enter image description here

  • Related