如何使用MatPlotLib打印分组数据?

2024-05-20 00:38:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有从facebook解析的数据,我想使用MatPlotLib进行绘图。我想看看我每年在线形图中使用某些单词的频率。我有这些数据,我希望看起来像下图,但是用MatPlotLib而不是Altair绘制。(不要担心标题或传奇)

下面的数据可以使用pandas-pd.DataFrame(df)转换为数据帧

df =  {'word': {2: 'lol',
  3: 'like',
  4: 'yeah',
  5: 'get',
  6: 'know',
  7: 'good',
  8: 'ur',
  9: 'ok',
  10: 'haha',
  11: 'man',
  12: 'lol',
  13: 'yeah',
  14: 'like',
  15: 'good',
  16: 'ok',
  17: 'ur',
  18: 'man',
  19: 'get',
  20: 'know',
  21: 'haha',
  22: 'yeah',
  23: 'lol',
  24: 'man',
  25: 'ok',
  26: 'like',
  27: 'good',
  28: 'get',
  29: 'ur',
  30: 'know',
  31: 'haha'},
 'count': {2: 9,
  3: 5,
  4: 5,
  5: 4,
  6: 3,
  7: 3,
  8: 2,
  9: 1,
  10: 1,
  11: 1,
  12: 57,
  13: 48,
  14: 36,
  15: 31,
  16: 23,
  17: 19,
  18: 17,
  19: 16,
  20: 11,
  21: 2,
  22: 129,
  23: 100,
  24: 52,
  25: 51,
  26: 50,
  27: 40,
  28: 35,
  29: 30,
  30: 27,
  31: 22},
 'year': {2: 2010,
  3: 2010,
  4: 2010,
  5: 2010,
  6: 2010,
  7: 2010,
  8: 2010,
  9: 2010,
  10: 2010,
  11: 2010,
  12: 2011,
  13: 2011,
  14: 2011,
  15: 2011,
  16: 2011,
  17: 2011,
  18: 2011,
  19: 2011,
  20: 2011,
  21: 2011,
  22: 2012,
  23: 2012,
  24: 2012,
  25: 2012,
  26: 2012,
  27: 2012,
  28: 2012,
  29: 2012,
  30: 2012,
  31: 2012}}

enter image description here


Tags: 数据绘图dfgetfacebookmatplotliboklike
1条回答
网友
1楼 · 发布于 2024-05-20 00:38:43

您想要pivot表格和绘图:

# convert to dataframe
df = pd.DataFrame(df)

df.pivot(index='year',columns='word',values='count').plot()

输出:

enter image description here

相关问题 更多 >