我正在关注本节,我意识到这段代码是使用Python 2编写的,但是它们在'Start Date'
轴上显示了xticks,而我没有。我的图表仅显示Start Date
,没有提供日期。
#设置as_index = False保留0,1,2,...索引。然后,我们将以当天的平均调查结果为准。
poll_df = poll_df.groupby(['开始日期'],as_index = False).mean()
# Let's go ahead and see what this looks like
poll_df.head()
Start Date Number of Observations Obama Romney Undecided Difference
0 2009-03-13 1403 44 44 12 0.00
1 2009-04-17 686 50 39 11 0.11
2 2009-05-14 1000 53 35 12 0.18
3 2009-06-12 638 48 40 12 0.08
4 2009-07-15 577 49 40 11 0.09
Great! Now plotting the Differencce versus time should be straight forward.
# Plotting the difference in polls between Obama and Romney
fig = poll_df.plot('Start Date','Difference',figsize=(12,4),marker='o',linestyle='-',color='purple')