python - 如何使用 Matplotlib 对 Pandas 数据框中的数据进行分类和绘图?

标签 python pandas python-2.7 dataframe matplotlib

我有一个包含推文值的 DataFrame,并且想要根据 'Date' 绘制 'Favourites' 的图表并进行分类/颜色-通过'User'对数据进行编码。

我能够获得数据的散点图或条形图,但无法获得基于“用户” 进行分类的有效解决方案。图表中的“日期”也显得很困惑,我无法理解此问题的原因。

我尝试过使用this tutorial获取折线图,但不明白如何将其应用到我的 DataFrame

数据帧结构

data_frame = pandas.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets'])

data_frame['User'] = numpy.array([tweet.user.screen_name for tweet in tweets])
data_frame['ID'] = numpy.array([tweet.id for tweet in tweets])
data_frame['Length'] = numpy.array([len(tweet.text) for tweet in tweets])
data_frame['Date'] = numpy.array([tweet.created_at for tweet in tweets])
data_frame['Source'] = numpy.array([tweet.source for tweet in tweets])
data_frame['Favourites'] = numpy.array([tweet.favorite_count for tweet in tweets])
data_frame['Retweets'] = numpy.array([tweet.retweet_count for tweet in tweets])

return data_frame

绘图

x = result.Date
y = result.Favourites

plt.xlabel("Date", fontsize=10)
plt.ylabel("Favourites", fontsize=10)


plt.figure(figsize=(30,30))

fig, ax = plt.subplots()


plt.scatter(x,y)

plt.savefig('plot.png')

我希望图表显示收藏夹与时间的折线图,并使用不同的用户颜色编码,如下例所示:this example

我当前的输出是这样的:this

示例数据

Output

Raw paste

最佳答案

如果不查看确切的数据,就很难提供解决方案。也许这值得一试。

for user in data_frame.User.unique():
     d = data_frame[data_framef['User']==user]
     plt.plot(d['Date'],d['Favourites'],'o')

plt.plot()

关于python - 如何使用 Matplotlib 对 Pandas 数据框中的数据进行分类和绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315796/

相关文章:

python - numpy 数组中的时间值到日期时间数组

python - Pandas :特定列的产品

python - 在 Centos 上安装 M2Crypto 时遇到问题

python - 如何从文件夹中选择带有编号扩展名的文件?

python - Qt WebEngine 中 QWebInspector 的替代品是什么?

python - Pandas 根据列计算顺序

python - 根据 pandas 数据框中的条件删除每组的最后一行

Python:如何创建二元组序列

python-2.7 - 麻烦让 tensorflow 工作,libcudart.so.8.0 不在路径中

python - 为什么python中会出现这个AttributeError?