使用Python和ggplot绘制平均值
我有以下代码生成的图表:使用Python和ggplot绘制平均值,python,graph,ggplot2,bar-chart,mean,Python,Graph,Ggplot2,Bar Chart,Mean,我有以下代码生成的图表: from pandas import * from ggplot import * plot = ggplot(data, aes('x','y')) \ #from dataframe 'data', columns x and y + geom_bar(stat='bar', fill='blue') + ggtitle('Graph of X and Y') \ +scale_x_continuous(name="X-Axis", breaks=[0, 4, 8
from pandas import *
from ggplot import *
plot = ggplot(data, aes('x','y')) \ #from dataframe 'data', columns x and y
+ geom_bar(stat='bar', fill='blue') + ggtitle('Graph of X and Y') \
+scale_x_continuous(name="X-Axis", breaks=[0, 4, 8, 12, 16, 20, 23], \
labels=["Midnight", "4:00am", "8:00am", "12:00pm","4:00pm","8:00pm","11:00pm"])\
+ylab("Y-Axis") + xlim(0, 23)
print plot
由y表示的变量是数周内每小时事件数的计数。我想检查平均每小时的事件数,而不是该时间段内每小时的事件总数
如何在Python中使用ggplot绘制“y”的平均值,而不是仅仅绘制“y”
谢谢
编辑:
所以,我想我真正需要的是一种获得每小时平均事件数(x)的方法。目前,当我尝试这样做时,我返回一个在所有小时内具有相等值的图形 我不确定y值在代码中来自何处,但对任何数据集求平均值的基本方法是将所有值相加,然后将总和除以值的数量 因此,您可以使用如下函数从值列表中生成平均值:
def average(list_):
output = 0
for i in list_;
output += i
output /= len(list_)
return output
谢谢你的帮助。我很快就试过了,但收到了一些错误。我会努力一点,让你知道它是如何进行的。