2021-11-28

Plotting Stackbar chart in Seaborn for showing clustering

Here is my data where I have Wines%, Fruits%, etc which sums up to 1 and is based on the Total_Spent column. There's also a cluster columns that you can see:

enter image description here

Now, I want to show a stacked bar chart where on the x axis I'll have the clusters and the vertical stacked bar will be all the wines%, meat%, etc for every cluster. Using this chart, I'll be able to observe which cluster is spending what percent of their money on which product. I'm trying to use seaborn for this. Can anyone help me in figuring out a way to plot this stacked bar plot?

Update

So I have written this code to get the data in the correct format:

df_test = df[['Wines', 'Fruits', 'Meat', 'Fish', 'Sweets','Gold', 'Clusters']]
df_unpivoted = df_test.melt(id_vars=['Clusters'], var_name='Category', value_name='Spend%')
df_unpivoted.head()
df_new = pd.pivot_table(df_unpivoted, index=['Clusters','Category'])

And the dataframe looks like this:

enter image description here

How can I achieve the same result with this dataframe now?



from Recent Questions - Stack Overflow https://ift.tt/317cEXt
https://ift.tt/3E0aZBo

No comments:

Post a Comment