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

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

How to visualize better separated columns

问题

我有这个数据框

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

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

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

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

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

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

英文:

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:
如何更好地可视化分隔明显的列

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的一个很好的使用案例:

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()

输出:

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

英文:

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:

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

答案2

得分: 0

早上好,

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

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()

这是结果图:

查看图像描述

英文:

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

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:

确定