我正在尝试在Seaborn中绘制分类条形图,其类别由DataFrame
(Favourites
)中的值设置。当我尝试将此值传递到hue
函数的barplot
关键字中时,Seaborn为什么会引发错误?
我试图将DataFrame分为两个不同的DataFrame,以便可以将它们绘制在一起,但只能得到堆叠的条形图,而不是并排的分类图。
我在Seaborn文档上看到,将类别列名称传递给hue
关键字可以对数据进行分类,但是当我尝试将Favourites
传递给它时,总是收到错误消息。
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])
data_frame['Day'] = data_frame['Date'].dt.day_name()
data_frame.set_index(['User'],inplace=True)
frames = [user1, user2]
result = pandas.concat(frames)
x = result.Day
y = result.Favourites
result.groupby('Day',as_index=False, sort=True, group_keys=True).agg({'Favourites':'sum'})
#user2.groupby('Day',as_index=False, sort=True, group_keys=True).agg({'Favourites':'sum'})
plotted = sns.barplot(x,y,color="skyblue",data=result,hue='User',order=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])
sns_plot = plotted.get_figure()
sns_plot.savefig('plot.png')
User Date Favourites Day
AOC 1/3/2019 500 Monday
AOC 2/3/2019 200 Tuesday
realDonaldTrump 3/3/2019 600 Wednesday
realDonaldTrump 4/3/2019 700 Thursday
我想要这样的条形图:
这是我收到的错误/堆栈跟踪:
问题出在DataFrame定义的这一行(我删除了),
data_frame.set_index(['User'],inplace=True)