如何更好地可视化分隔明显的列

huangapple go评论97阅读模式
英文:

How to visualize better separated columns

问题

我有这个数据框

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

对于每个不同的seed_word,我想知道literal和metaphoric的大小。

为了获得这些信息,我编写了以下代码:

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

并得到了这个图表:
如何更好地可视化分隔明显的列

问题是,我希望每个seed word有相同的柱状图,其中一种颜色代表metaphoric,另一种颜色代表literal。希望我解释得清楚。

英文:

I have this dataframe

  1. SEED_WORD Metaphoric_Seed size
  2. 0 activity literal 9
  3. 1 activity metaphoric 1
  4. 2 allow literal 1
  5. 3 allow metaphoric 1
  6. 4 back literal 3
  7. 5 backbone literal 4
  8. 6 backbone metaphoric 12
  9. 7 base literal 14
  10. 8 bracket literal 2
  11. 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:
如何更好地可视化分隔明显的列

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

答案1

得分: 0

这是seaborn的一个很好的使用案例:

  1. import pandas as pd
  2. import seaborn as sns
  3. import matplotlib.pyplot as plt
  4. 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}})
  5. sns.barplot(df, x='SEED_WORD', y='size', hue='Metaphoric_Seed')
  6. plt.show()

输出:

如何更好地可视化分隔明显的列

英文:

This is a good use case for seaborn:

  1. import pandas as pd
  2. import seaborn as sns
  3. import matplotlib.pyplot as plt
  4. 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}})
  5. sns.barplot(df, x='SEED_WORD', y='size', hue='Metaphoric_Seed')
  6. plt.show()

Output:

如何更好地可视化分隔明显的列

答案2

得分: 0

早上好,

就我看来,我认为你想要制作一个直方图,所以这里是一个用于制作堆叠直方图的代码。不过我认为这段代码可以大幅简化。

  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. df = pd.read_excel('data.xlsx')
  4. df = df.set_index('SEED_WORD')
  5. df_meta = df[df['Metaphoric_Seed'] == "metaphoric"]
  6. df_lit = df[df['Metaphoric_Seed'] != "metaphoric"]
  7. index = []
  8. for k in df_meta.index.tolist():
  9. index.append(df_lit.index.tolist().index(k))
  10. plt.bar(df_lit.index.unique().tolist(), df_lit['size'], label='Literal')
  11. plt.bar(df_meta.index.unique().tolist(), df_meta['size'], bottom=[df_lit['size'][j] for j in index], label='Metaphoric')
  12. jours = df.index.unique().tolist()
  13. plt.xticks(range(len(jours)), jours, rotation=45)
  14. plt.legend()
  15. plt.show()

这是结果图:

查看图像描述

英文:

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

  1. df = pd.read_excel('data.xlsx')
  2. df =df.set_index('SEED_WORD')
  3. df_meta = df[df['Metaphoric_Seed'] == "metaphoric"]
  4. df_lit = df[df['Metaphoric_Seed'] != "metaphoric"]
  5. index =[]
  6. for k in df_meta.index.tolist():
  7. index.append( df_lit.index.tolist().index(k))
  8. plt.bar( df_lit.index.unique().tolist(),df_lit['size']
  9. , label = 'Literal')
  10. plt.bar( df_meta.index.unique().tolist(),df_meta['size'],
  11. bottom = [df_lit['size'][j] for j in index],
  12. label = 'Metaphoric')
  13. jours = df.index.unique().tolist()
  14. plt.xticks(range(len(jours)),jours, rotation = 45)
  15. plt.legend()
  16. plt.show()

''
here is the result
enter image description here

huangapple
  • 本文由 发表于 2023年3月15日 19:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定