0

I have this dataframe

SEED_WORD Metaphoric_Seed  size
0    activity         literal     9
1    activity      metaphoric     1
2       allow         literal     1
3       allow      metaphoric     1
4        back         literal     3
5    backbone         literal     4
6    backbone      metaphoric    12
7        base         literal    14
8     bracket         literal     2
9   establish         literal     4`

For each distinct seed_word, I would like to have the size of literal and metaphoric.

To get it, I coded:

df_all_annotated.groupby(['SEED_WORD','Metaphoric_Seed'])['SEED_WORD','Metaphoric_Seed'].count().plot.bar()

And got this: graph

The problem is that I would like the same bar for each seed word, in a way that one color is for metaphoric and one is for the literal. I hope I explained everything well

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • There's no need to use seaborn. Simply pivot the the dataframe: `ax = df.pivot(index='SEED_WORD', columns='Metaphoric_Seed', values='size').plot(kind='bar', rot=0, figsize=(8, 6))` [see plot result](https://i.stack.imgur.com/w8qsQ.png) – Trenton McKinney Mar 15 '23 at 18:46

2 Answers2

0

This is a good use case for seaborn:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'SEED_WORD': {0: 'activity', 1: 'activity', 2: 'allow', 3: 'allow', 4: 'back', 5: 'backbone', 6: 'backbone', 7: 'base', 8: 'bracket', 9: 'establish'}, 'Metaphoric_Seed': {0: 'literal', 1: 'metaphoric', 2: 'literal', 3: 'metaphoric', 4: 'literal', 5: 'literal', 6: 'metaphoric', 7: 'literal', 8: 'literal', 9: 'literal'}, 'size': {0: 9, 1: 1, 2: 1, 3: 1, 4: 3, 5: 4, 6: 12, 7: 14, 8: 2, 9: 4}})

sns.barplot(df, x='SEED_WORD', y='size', hue='Metaphoric_Seed')
plt.show()

Output:

enter image description here

Tranbi
  • 11,407
  • 6
  • 16
  • 33
0

Good morning,

for my part i thought you wanted to make a histogram so here is a code to make a stacked histogram. but i think this code can be well shortened '' import matplotlib.pyplot as plt import pandas as pd

df = pd.read_excel('data.xlsx')
df =df.set_index('SEED_WORD')

df_meta = df[df['Metaphoric_Seed']  == "metaphoric"]
df_lit = df[df['Metaphoric_Seed']  != "metaphoric"]
index =[]
for k in df_meta.index.tolist():
    index.append( df_lit.index.tolist().index(k))
    
plt.bar( df_lit.index.unique().tolist(),df_lit['size']
        ,  label = 'Literal')
plt.bar( df_meta.index.unique().tolist(),df_meta['size'],
        bottom = [df_lit['size'][j] for j in index],
        label = 'Metaphoric')
 
jours = df.index.unique().tolist()
plt.xticks(range(len(jours)),jours, rotation = 45)

plt.legend()
plt.show()

'' here is the result enter image description here

  • fwiw it can also be achieved with seaborn: `sns.histplot(df, x='SEED_WORD', weights='size', hue='Metaphoric_Seed', multiple='stack', shrink=0.8)` – Tranbi Mar 15 '23 at 14:24